Click here to Skip to main content
15,880,725 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
He , so i am writing a lot of code.
And now i wanna know if i have written all the dispose's.

Like for example :
private void DoDadaBoom()
{
   var bitmap = new bitmap(params)
   DoStuff(bitmap)
   dode = new bitmap.clone();
   bitmap.dispose()
}


But :
Maybe i could have forgotten to write 'bitmap.dispose()'.

I wanna trick to get a good overview > for every potentionally forgotten dispose call.

Any ideas ?
Posted

Following article of our site will be useful to you.

Finding Undisposed Objects[^]

Hope this will help!
 
Share this answer
 
I think you should not worry about undisposed objects. That is the GarbageCollector's job. If an object has no references to it then the garbage colector automatically disposes it.

But if you just want to call the dispose method, whenever you use an IDisposable object, suround it with the using keyword:

private void DoDadaBoom(){   
   using(var bitmap = new bitmap(params))
   {   
        DoStuff(bitmap);
        dode = new bitmap.clone();
   }
}


The using keyword disposes the object at the end of the using block.
 
Share this answer
 
Comments
Pete O'Hanlon 12-May-10 15:21pm    
Undisposed objects actually are something to worry about. If you don't dispose of something that uses unmanaged resources then you might not actually free those resources up. You are relying on a finalizer tidying those up.
Jarno Burger 30-Jul-10 14:41pm    
jip , i am playing with direct3d and directshow , thats why , but i am getting the hang of it now :)
Hi Jarno,

Try the solution given by Jinal Desai. It will help you to find the disposable object in your project.

Lastly, one suggestion from me i.e. don't call the dispose method instead surround the instance of the object with using(....) {} like below:

Suppose, you have a disposable object which inherited from IDisposable interface. Work with the instance of the class like this:

using(Person p = new Person())
{
.
.
.
}

Here, you don't have to call the Dispose() method explicitly. The runtime will take of it. Hope this information will help you.

Remember: Please "Mark As Answer" if this helps you.

Regards,
Kunal
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900