You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.ObjectDisposedException in Tensorflow dll #984
I got some error when my application was closed.
The compiler return as follows:
未處理的例外狀況: System.ObjectDisposedException: 無法存取已處置的物件。
物件名稱: 'ThreadLocal 物件已被處置。'。
於 System.Threading.ThreadLocal`1.GetValueSlow()
於 Tensorflow.Variables.EagerResourceDeleter.DisposeUnmanagedResources(IntPtr handle)
於 Tensorflow.DisposableObject.Dispose(Boolean disposing)
於 Tensorflow.DisposableObject.Finalize()
Is it possible that the TensorFlow object will auto-delete by itself?
Actually, I just call the third-party Lib(It call Tensorflow.net).
When the training model flow was finished, then I close the application, then error happened!
Maybe the Tensor A is the handle of the Tensor B. Then Tensor B still try to dispose Tensor A while Tensor A has already been disposed. However, though I'm more than happy to help you, I can't reproduce it without any code.
Would you mind providing a code that can reproduce the error? You can modify your model definition to protect your privacy. Thank you!
EagerResourceDeleters DisposeUnmanagedResources uses tf.Context which is another finalizable type - ThreadLocalStorage. That type might be finalized before EagerResourceDeleter and if so it will cause a crash the same as was hit in #912 with Session.
An object has to be super careful about what it puts in a finalizer. The only guarantee is that it can access data in base classes and itself. Value types are safe. Any reference type may not be safe if it too is finalizable - it may be finalized before the current type.
Fix here would be to either remove the DisposeUnmanagedResources or have the type capture whatever state it needs as a local that's not a finalizable object.
Activity
SanftMonster commentedon Feb 15, 2023
It seems to be caused by double dispose. Could you please provide a minimal example that can reproduce this error?
DoraAHsu commentedon Feb 16, 2023
Is it possible that the TensorFlow object will auto-delete by itself?
Actually, I just call the third-party Lib(It call Tensorflow.net).
When the training model flow was finished, then I close the application, then error happened!
SanftMonster commentedon Feb 16, 2023
Maybe the Tensor A is the
handleof the Tensor B. Then Tensor B still try to dispose Tensor A while Tensor A has already been disposed. However, though I'm more than happy to help you, I can't reproduce it without any code.Would you mind providing a code that can reproduce the error? You can modify your model definition to protect your privacy. Thank you!
Oceania2018 commentedon Mar 5, 2023
Do you mind if tell the name of the third-party lib which calls tensorflow.net?
ericstj commentedon Jul 11, 2025
I found this same bug.
EagerResourceDeletersDisposeUnmanagedResourcesusestf.Contextwhich is another finalizable type -ThreadLocalStorage. That type might be finalized beforeEagerResourceDeleterand if so it will cause a crash the same as was hit in #912 with Session.TensorFlow.NET/src/TensorFlowNET.Core/Variables/EagerResourceDeleter.cs
Lines 24 to 26 in 6ce6066
An object has to be super careful about what it puts in a finalizer. The only guarantee is that it can access data in base classes and itself. Value types are safe. Any reference type may not be safe if it too is finalizable - it may be finalized before the current type.
Fix here would be to either remove the
DisposeUnmanagedResourcesor have the type capture whatever state it needs as a local that's not a finalizable object.