0

I have a non static class that deals with some unmanaged code, my class inherits IDisposable which requires that the class implements the Dispose() method. How can I make this require Dispose() is called on an instance of this class, not just implemented in the class itself?

public class MyClass : IDisposable
{
    //Some fields and methods

    public void Dispose()
    {
        //Code that frees unmanaged resourses
    }
}

In my main method:

public void main()
{
    var myClassInstance = new MyClass();

    //Some code that does stuff with myClassInstance

    //I forget here to call myClassInstance.Dispose();

}

I now want to be given a warning. For Example: "You have not called Dispose on myClassInstance".

gegs
  • 3
  • 2
  • What do you mean by "given a warning"? A compiler warning? – Blorgbeard Apr 10 '19 at 21:55
  • @Blorgbeard I believe they mean https://www.bing.com/search?q=c%23+dispose+enforce (which I picked couple duplicates for) – Alexei Levenkov Apr 10 '19 at 21:55
  • 2
    You can't, really. How can you be sure when your class _needs_ to be disposed? You basically want to predict when the instance is going out of scope. – Mike G Apr 10 '19 at 21:55
  • @MikeTheLiar It's worse than that. You need to predict when the lifetime of the object ends, not when the variable goes out of scope. The variable could go out of the scope but the object still be used elsewhere, or the same variable used for multiple disposable objects. – Servy Apr 10 '19 at 22:00
  • @MikeTheLiar Essentially I just want a warning stating Dispose hasn't been called anywhere. I'm writing a game and these resources should only be in scope for a couple of frames. – gegs Apr 10 '19 at 22:02
  • 2
    @Servy surely we can just analyze the program itself, determine when it will terminate, and then throw an error if the object hasn't been disposed of at that point? – Mike G Apr 10 '19 at 22:03
  • You can build a roslyn analyzer and ship it with your lib – Kalten Apr 10 '19 at 22:09

1 Answers1

-1

Put the message in the destructor ~MyClass and mark it with the [Debug] attribute. You won't be able to control when it is called but you will get the message the first time one of them is garbage collected.

sjb-sjb
  • 1,112
  • 6
  • 14
  • "You won't be able to control when it is called" so this is effectively not an answer to the question asked – Camilo Terevinto Apr 10 '19 at 22:17
  • It may not answer the question but I think it addresses the need, at least as far as practically possible. So let's see what the OP (@gegs) says before downvoting. – sjb-sjb Apr 10 '19 at 22:24
  • @sjb-sjb I agree, with the duplicates read and the comments here it seems it's not possible in the exact way I asked, at least not in a recommended/ non hacked together way. So I will mark this answer. – gegs Apr 10 '19 at 22:36
  • Thanks @gegs for marking as answer. Comment, there are a lot of ways that mem leaks and resource leaks can occur. For example, failing to unsubscribe a short-lived object from an event of a long-lived object. Although c# offers memory managmt, IDisposable is a contract saying that the programmer, rather than the system, will manage the release of resources. So my suggestion of using the destructor to check for Dispose is just a debugging trick, not something to rely on for production. Some people claim that one should always check for Dispose in the destructor, but I am not a fan of that. – sjb-sjb Apr 11 '19 at 15:33