Archived

This topic is now archived and is closed to further replies.

edwinnie

C++.net dispose(), how convincing??

Recommended Posts

okok i was fiddling around codes by having the object able to release both managed and unmanaged resources. But somehow i wonder, since the destructor was not called due to suppressfinalize, how convincing is that the object is actually deleted and memory reclaimed? (bearing in mind that we have suppressfinalize called) code i used(taken from MSDN):
MyObject::MyObject() 
{ 
   disposed = false; 
} 
MyObject::~MyObject() 
{ 
   Dispose(false); 
} 
void MyObject::Dispose() 
{ 
   Dispose(true); 
   GC::SuppressFinalize(this); 
} 
void MyObject::Dispose(bool disposing) 
{ 
  //Check to see if Dispose has already been called 

  if(!disposed) 
  { 
      if(disposing) 
      { 
           //Dispose managed resources here 

      } 
     
   //Release unmanaged resources 

   //If disposing is false, only the following code is executed 

   } 
   disposed = true; 
} 
need some more understanding... thx! Edwinz

Share this post


Link to post
Share on other sites
The IDisposable interface (giving the Dispose() method) is
supposed to be used in conjuction with the "using" statement.


using (MyObject obj = new MyObject())
{
// Usg obj here

}
// end using


// Now the object is released



As soon as it goes out of scope, the Dispose() method is
called and resources reclaimed.

Otherwise, the normal destructor is called, whenever the
GC wants to do it.




Kami no Itte ga ore ni zettai naru!

Share this post


Link to post
Share on other sites