-1

I tried to clean up memory of unmanaged classes from c++/cli finalizer and it gave me AcessViolation error. I do something like this:

MyClass::!MyClass()
{

    if(_unmanaged)
    {
    _unmanaged->Delete();
    _unmanaged = 0;
    }
}

Could anyone know, what the problem? Thanks for answers.

Ivan_Deev
  • 1
  • 2

1 Answers1

0

Ok, I have one guess.

Finalizer works from it's own thread, so when you try to dispose your object manually race conditions can occur.

I saw your comment about "already deleted object" but I would check it once again.

MyClass::~MyClass()
{
   this->!MyClass();

   GC::KeepAlive(this);
}
Eugene
  • 3,335
  • 3
  • 36
  • 44
  • The finalizer works on its own thread, but if something has a reference to the object, which is required in order to call Dispose, then the garbage collector won't touch it. – David Yaw Apr 26 '12 at 15:53