Hangar's picture

GameWindow.Dispose, FxCop [resolved]

Maybe this is fixed in SVN, but I noticed some problems with GameWindow's Dispose implementation when trying to subclass it. The main Dispose() method should be public and sealed, call GC.SuppressFinalize() and the protected virtual Dispose(bool). Also, neither of these functions should throw exceptions. (ObjectDisposeException is supposed to be raised on other methods and properties after disposal, but Dispose should be repeatable).

FxCop might help for some things like this. http://code.msdn.microsoft.com/codeanalysis/Release/ProjectReleases.aspx... Mostly it's annoying (might want to disable the naming rules for OpenTK) but it's helped to clean up my own project a lot.


Comments

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Hangar's picture

Got a workaround for now, but it's a little ugly. (Boo code.)

transient class GameWindowAdapter(GameWindow, IDisposable):
"""Adaptation to fix disposable issues."""
 
	final override def Dispose():
	"""Invoke disposal."""
		Dispose(true)
		GC.SuppressFinalize(self)
 
	def destructor():
	"""Automatically invoke disposal."""
		Dispose(false)
 
	protected virtual def Dispose(manual as bool):
	"""
	Clean up unmanaged resources.
	Param manual: Whether this was manually called.
	"""
		super.Dispose()
the Fiddler's picture

You are of course correct on the ObjectDisposedException.

What I'm not so sure about is the Dispose(bool) method being protected instead of private - i.e. what kind of resources would you need to destroy on GameWindow *disposal*, that you can't destroy in the Unload event? I may be wrong here, but I usually think of Dispose(bool) to be an implementation detail, not as a part of the API contract.

One last thing, there's actually no need for a finalizer in the GameWindow class: it doesn't hold unmanaged any resources itself and the classes which *do*, e.g. GraphicsContexts or NativeGLWindows, all have finalizers.

Hangar's picture

edit: Ignore what was posted here if you saw it. Been relying on third-party resources, but I finally found some official guidelines: http://www.bluebytesoftware.com/blog/PermaLink.aspx?guid=88e62cdf-5919-4...

The pattern to follow in this case is Simple Example w/out Finalize (C#) under 1.1.2. No need for a destructor or call to GC.SuppressFinalize, but you should make Dispose(void) public and sealed, whereas Dispose(bool) should be protected and virtual.

the Fiddler's picture

Thanks for the link, this was very informative. I'll update OpenTK to follow these guidelines wherever it makes sense :)

Alright, GameWindow is patched up in SVN.