Fixed issue with memory-freeing callback delegates being garbage-collected in Windows.
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
Try to create a pull request from your fork: https://help.github.com/articles/creating-a-pull-request-from-a-fork/
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
Thanks for catching this bug and reporting it!
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 .
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.
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 .
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 ;-)