Skip to content

System.ObjectDisposedException in Tensorflow dll #984

Open

Description

@DoraAHsu

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()

How to fix this error?

Activity

  1. SanftMonster commented on Feb 15, 2023

    @SanftMonster
    Collaborator

    It seems to be caused by double dispose. Could you please provide a minimal example that can reproduce this error?

  2. DoraAHsu commented on Feb 16, 2023

    @DoraAHsu
    Author

    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!

  3. SanftMonster commented on Feb 16, 2023

    @SanftMonster
    Collaborator

    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!

  4. Oceania2018 commented on Mar 5, 2023

    @Oceania2018
    Member

    I just call the third-party Lib

    Do you mind if tell the name of the third-party lib which calls tensorflow.net?

  5. ericstj commented on Jul 11, 2025

    @ericstj

    I found this same bug.

    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.

    tf.Runner.TFE_Execute(tf.Context, _handle_device, "DestroyResourceOp",
    new[] { _tensor },
    new object[] { "ignore_lookup_error", true }, 0);

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions