Chapter 7 Quick Reference


Do this

Provide a finalizer for a class.

Implement a C++ destructor. For example:

~MyClass() { // Put cleanup code here }

Provide a Dispose method for a class.

Make your class inherit from the IDisposable interface, and implement the Dispose method. For example:

__gc class MyClass : public IDisposable { public: void Dispose() { // Put cleanup code here } }

Integrate Finalize and Dispose.

Provide a Boolean flag that is set when the resources have been released. Set this in Dispose, and check it in the destructor.

Ensure that methods aren’t called on objects that have had their resources reclaimed.

Check the flag that tells you whether the object’s resources have been reclaimed. If they have, the object is dead and you should throw an ObjectDisposedException to tell the caller that the object is dead.

Force a garbage collection.

Call the System::GC::Collect method.

Find out what garbage collector generation an object belongs to.

Call the System::GC::GetGeneration method, passing in a reference to the object.

Microsoft Visual C++  .NET(c) Step by Step
Microsoft Visual C++ .NET(c) Step by Step
ISBN: 735615675
Year: 2003
Pages: 208 © 2008-2017.
If you may any questions please contact us: