Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

[.net] Finalize() and Dispose()


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 iliak   Members   -  Reputation: 264

Like
0Likes
Like

Posted 01 May 2010 - 09:25 PM

I my framework, to be sure to call Dispose() on my classes, I do the following :
public class Foo : IDisposable
{
	public Foo()
	{
		GL.GenBuffers(1, out Handle);
	}

	public ~Foo()
	{
		if (Handle != -1)
			throw new Exception("Handle != -1, Call Dispose() !!");
	}
	
	public void Dispose()
	{
		GL.DeleteBuffers(1, ref Handle);
		Handle = -1;

		GC.SuppressFinalize(this);
	}
	
	int Handle;
}

Am I right using this method (be sure Dispose() is called) ?
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]

Ad:

#2 Rattenhirn   Members   -  Reputation: 1173

Like
0Likes
Like

Posted 01 May 2010 - 11:11 PM

"GC.SuppressFinalize(this);" prevents the GC from calling the finalizer, so the "if" shouldn't even be needed. However, I'm not sure how useful throwing an exception in the finalizer is...

The better way to handle this is to simply call "Dispose" in the finalizer, as suggested in the docs of IDisposable.

Calling "Dispose" manually should never be mandatory, just an optimization.

#3 iliak   Members   -  Reputation: 264

Like
0Likes
Like

Posted 02 May 2010 - 12:23 AM

Not in my case (OpenGL) as the GL context is invalid when calling Finalize()/Destructor.
- Iliak -
[ ArcEngine: An open source .Net gaming framework ]
[ Dungeon Eye: An open source remake of Eye of the Beholder II ]

#4 Rattenhirn   Members   -  Reputation: 1173

Like
0Likes
Like

Posted 02 May 2010 - 01:39 AM

Quote:
Original post by iliak
Not in my case (OpenGL) as the GL context is invalid when calling Finalize()/Destructor.


I see. In that case I suggest the following:
Since those GL buffers are created and destroyed via the GL class, it knows how many of them exist. As soon as the GL context is about to be invalidated, check if there are any leftover buffers and throw an exception in this case.

As an extension, you could also save the callstack of each GL.GenBuffers with the created buffer (at least in debug mode), in order to provide the user very useful info where the resource leaks have originated.

Again, do not use the .net finalizers for error reporting, or really, for anything. Here's an article about that topic, I'm sure there are many more.

#5 kanato   Members   -  Reputation: 568

Like
0Likes
Like

Posted 02 May 2010 - 06:04 PM

In your finalizer, you should store the handle ID in an external list and then check that list periodically and release the buffers in that list. Or if you want to throw an exception for an unreleased resource, you should do it there instead of the finalizer thread.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS