TensorFlowSharp icon indicating copy to clipboard operation
TensorFlowSharp copied to clipboard

Fixed issue with memory-freeing callback delegates being garbage-collected in Windows.

Open JohnTenney opened this issue 9 years ago • 7 comments

Frankly, not sure how to submit a pull request. So here's the info: on Windows, certain delegates that are used to free memory are being garbage collected, resulting in exceptions when trying to dispose TFTensor or TFBuffer classes. Attached are the fixes I made that resolved the issues. TensorFlowSharp fixes.zip

Had to make some minor changes, primarily to static constructors: Changed   static TFBuffer ()               {                      FreeBufferFunc = Marshal.GetFunctionPointerForDelegate (FreeBlock);               }   to static TFBuffer ()               {             BufferReleaseFunc del = (data, length) => FreeBlock(data, length);             GCHandle.Alloc(del);             FreeBufferFunc = Marshal.GetFunctionPointerForDelegate (del);               }   Also have to add the following code to TFTensor (and then replace use of FreeTensorHandle in the rest of the file with FreeTensorHandleFunc, and FreeTensorData with FreeTensorDataFunc):         static Deallocator FreeTensorHandleFunc;         static Deallocator FreeTensorDataFunc;         static TFTensor()         {             FreeTensorHandleFunc = (data, size, deallocatorData) => FreeTensorHandle(data, size, deallocatorData);             GCHandle.Alloc(FreeTensorHandleFunc);             FreeTensorDataFunc = (data, len, closure) => FreeTensorData(data, len, closure);             GCHandle.Alloc(FreeTensorDataFunc);        

JohnTenney avatar Jul 25 '17 18:07 JohnTenney

Try to create a pull request from your fork: https://help.github.com/articles/creating-a-pull-request-from-a-fork/

Dorokhov avatar Jul 27 '17 08:07 Dorokhov

I applied a simpler version of this patch, I keep the delegate on a static field:

https://github.com/migueldeicaza/TensorFlowSharp/commit/46bea6c02e7d4c720bce034532e00020a2831f88 https://github.com/migueldeicaza/TensorFlowSharp/commit/daec306fff9aee95fc5a379a259f61ab489738a0

migueldeicaza avatar Jul 27 '17 15:07 migueldeicaza

Thanks for catching this bug and reporting it!

migueldeicaza avatar Jul 27 '17 15:07 migueldeicaza

Thanks, guys. I didn't find keeping the delegate on a static field was good enough to prevent garbage collection (even though it's a static field, it is never referred to after the static constructor--maybe Windows VM 'helps' you out by getting rid of it), and it looks like you only did it in TFBuffer but not in TFTensor class. So I figured the system out well enough to submit a pull request. Let me know if you need a test program, though I'm not sure if you'll observe the problem on non-Windows machines (I have the latest Framework 4.7 installed on Windows 10, if that helps).

By the way, I want to say what a great help this software has been to us. With just of few lines of code, I've been able to pass images from Emgu's OpenCV wrapper to Tensorflow and perform inference on the GPU. Took a few hours in pure C#. Fantastic stuff!

Thanks, John

On Thu, Jul 27, 2017 at 8:19 AM, Miguel de Icaza [email protected] wrote:

I applied a simpler version of this patch, I keep the delegate on a static field:

46bea6c https://github.com/migueldeicaza/TensorFlowSharp/commit/46bea6c02e7d4c720bce034532e00020a2831f88 daec306 https://github.com/migueldeicaza/TensorFlowSharp/commit/daec306fff9aee95fc5a379a259f61ab489738a0

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/migueldeicaza/TensorFlowSharp/issues/104#issuecomment-318394154, or mute the thread https://github.com/notifications/unsubscribe-auth/AFXn9sWkmWtoyv1R1Ip-vWvfheeatHmlks5sSKpmgaJpZM4Oi6c4 .

JohnTenney avatar Jul 28 '17 03:07 JohnTenney

I used two commits, one did the buffer, one did the tensor. I do not think that Windows is optimizing that away, if the issue persists, can you share what kind of error you are getting?

I have Windows machine, I can test if you can share the program, and my team tells me that this should also be debuggable on Linux with Mono, where we have a debug flag to warn about these errors.MONO_DEBUG=keep-delegates Would love to see what is happening.

Very happy to hear that this binding has proved useful to you.

migueldeicaza avatar Jul 28 '17 23:07 migueldeicaza

I too (eventually) figured out your fix. Think we're good.

Thanks!

John

On Fri, Jul 28, 2017 at 4:58 PM, Miguel de Icaza [email protected] wrote:

I used two commits, one did the buffer, one did the tensor. I do not think that Windows is optimizing that away, if the issue persists, can you share what kind of error you are getting?

I have Windows machine, I can test if you can share the program, and my team tells me that this should also be debuggable on Linux with Mono, where we have a debug flag to warn about these errors.MONO_DEBUG=keep-delegates Would love to see what is happening.

Very happy to hear that this binding has proved useful to you.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/migueldeicaza/TensorFlowSharp/issues/104#issuecomment-318787106, or mute the thread https://github.com/notifications/unsubscribe-auth/AFXn9g2biW8DIgavfiUzismUy5aypUqIks5sSnWcgaJpZM4Oi6c4 .

JohnTenney avatar Jul 29 '17 02:07 JohnTenney

Excellent! Also - nice side effect, I have been using this idiom in my code in various places, and once you pointed out this bug, I went and fixed that in other projects ;-)

migueldeicaza avatar Jul 31 '17 20:07 migueldeicaza