Note that there are some explanatory texts on larger screens.

plurals
  1. POFinalizer Throws Random Exceptions, Raises Random Errors, Hangs App
    primarykey
    data
    text
    <p>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).</p> <p>Under the advice I got from <a href="http://msdn.microsoft.com/en-us/library/ms177197.aspx" rel="nofollow">MSDN</a> I'm cleaning up the unmanaged resources in the finalizer (!Fiber()), and the destructor (~Fiber()) is calling the finalizer.</p> <p>Here's the code:</p> <pre><code>Fiber::~Fiber () { this-&gt;!Fiber(); } Fiber::!Fiber () { if (thread!=NULL) { delete thread; thread=NULL; } if (fiber!=NULL) { DeleteFiber(fiber); fiber=NULL; } } </code></pre> <p>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:</p> <pre><code>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() </code></pre> <p>That error can also come from the line:</p> <pre><code>delete thread; </code></pre> <p>As well.</p> <p>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).</p> <p>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...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload