
VBOs and the Garbage Collector
Posted Friday, 27 August, 2010 - 13:43 by exoide inHi there,
I've a VBO's class like this
public class VBO : IDisposable { ~VBO() { Dispose(); } ///<summary> /// Release the memory used by the VBO ///</summary> public void Dispose() { GL.DeleteBuffers(amountBuffers, vboBuffersID); } }
It works nice when I remove the destructor from the code and call the Dispose() method before create a new instance of the class. Here is an example
public void foo() { VBO v = new VBO(); // Remove the previous VBO's data v.Dispose(); // Create the new VBO v = new VBO(); }
But if I don't remove the destructor and the garbage collector call the destructor I get the following error:
"Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
Here's an example
public void foo() { VBO v = new VBO(); //v.Dispose(); // In this way the previous VBO's data is removed once the destructor is invoked by the garbage collector // Create the new VBO v = new VBO(); }
Can someone explain me why is this happening?
Thanks


Comments
Re: VBOs and the Garbage Collector
Unlike C++ destructors, .Net finalizers run on a different thread that does not have access to OpenGL. Either remove the finalizer completely or use it to log a resource leak. (You can attempt more complicated approaches, such as resurrecting the object to add it to a "dispose queue" that is periodically emptied from the main thread, but I don't think that's worth the effort).
Note that there are no guarantees that a finalizer will run. The rule of thumb is to design your code so that it still works even if no finalizers are called.
Re: VBOs and the Garbage Collector
exoide, if you are comming from C/C++ new to C#, there is a good free guide for such a situation:
.NET Book Zero
What the C or C++ Programmer Needs to know about C# and the .NET Framework
You can find it here: http://www.charlespetzold.com/books.html
Re: VBOs and the Garbage Collector
Yes you're right I'm coming from C++ new to C# so I'll read that book right away.
Thanks you all guys for your help.