Finalizer Throws Random Exceptions, Raises Random

2019-08-20 03:47发布

问题:

I have a class in C++/CLI that uses unmanaged resources (a HANDLE for a native thread (i.e. from CreateThread()) and an LPVOID for a fiber from CreateFiber/ConvertThreadToFiber).

Under the advice I got from MSDN I'm cleaning up the unmanaged resources in the finalizer (!Fiber()), and the destructor (~Fiber()) is calling the finalizer.

Here's the code:

Fiber::~Fiber () {

    this->!Fiber();

}


Fiber::!Fiber () {

    if (thread!=NULL) {

        delete thread;
        thread=NULL;

    }

    if (fiber!=NULL) {

        DeleteFiber(fiber);
        fiber=NULL;

    }

}

I have a test app that creates two fibers, tests them, and then disposes them as it's done with them. The first one is disposed just fine. The last one is disposed as the last line of the program, and it crashes out in one of three different ways:

Unhandled Exception: System.AccessViolationException: Attempted to read or write
 protected memory. This is often an indication that other memory is corrupt.
   at DeleteFiber(Void* )
   at System.Threading.Fiber.!Fiber()
   at System.Threading.Fiber.Dispose(Boolean )
   at System.Threading.Fiber.Finalize()

That error can also come from the line:

delete thread;

As well.

It may also crash with an OutOfMemoryException, or by hanging for a while, saying that the program experienced a stack overflow, and then hanging the console (I have to close cmd.exe and restart it to recover).

If I comment the destructor/finalizer out, and run the program, it runs perfectly, but that's not an option because I don't want unmanaged resources hanging around until the program ends...

回答1:

  1. If thread is a HANDLE, you clean it up with CloseHandle(thread), not delete thread.
  2. You should initialize thread and fiber to NULL in Fiber's constructor, to maintain the invariants of the class.
  3. You can't call DeleteFiber on the currently executing fiber, unless you want to terminate the thread. You clean it up by calling ConvertFiberToThread()