1

This question apply to a C# object that is a subclass of an Android View. Android Views are C# objects bounds to the lifetime of the underlying Java 'Object' class which provides a standard Dispose() pattern.

Is it possible that a C# object is disposed without Dispose(true) being ever called ?

I've verified that only Dispose(false) is ever called. And when it's called, the C# properties are unavailable (already disposed). But Dispose(true) is never called.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Softlion
  • 12,281
  • 11
  • 58
  • 88
  • Of course it's possible. If you're really asking how it's possible for a specific class, you'll probably need to provide more code with an explanation of why you think it shouldn't be possible. – StriplingWarrior Mar 28 '19 at 16:21
  • "disposed" and `Dispose(true)` are two different things; if the implementation of `Dispose()` is to call `Dispose(true)`, then if `Dispose(true)` isn't being called: *you didn't dispose it* - you dropped it on the floor and it got *finalized*; finalized is very different to disposed – Marc Gravell Mar 28 '19 at 16:23
  • @Softlion definitely a duplicate. All .NET objects are garbage collected, whether you call a method that just happens to be called `Dispose` or not. It could be named `Potato`. When an object is garbage collected, its finalizer is called, if it exists, to perform cleanup. `IDisposable` is an *extra* interface used to clean up expensive resources without waiting for the finalizer. To avoid duplication and leaks, it's good practice to put code that needs to be called in all cases in the `Dispose` method. That code is typically used to clean up *unmanaged* resourses. BUT ... – Panagiotis Kanavos Mar 28 '19 at 16:24
  • Definetly not a duplicate – Softlion Mar 28 '19 at 16:25
  • @Softlion duplicate and asking for a tutorial. All this is explained in the docs that show how to implement IDisposable – Panagiotis Kanavos Mar 28 '19 at 16:27
  • @Softlion continuing. When the *finalizer* is called, some of the *managed* resources (ie classes) held by an object may have been gc'd already. That's why the code needs to know how it was called - due to an explicit call to `Dispose()`? Or because the finalizer was called? That's why the `Dispose(bool)` method is used. Depending on the flag, `Dispose(bool)` should clean up all resources or only the unmanaged ones. `Dispose()` will call `Dispose(true)` for that. The finalizer though will call `Dispose(false)` – Panagiotis Kanavos Mar 28 '19 at 16:29
  • @Soflion there are *many* duplicates for this question. [Proper use of the IDisposable interface](https://stackoverflow.com/questions/538060/proper-use-of-the-idisposable-interface/538238) is pehaps a better duplicate. Another one is [Finalize/Dispose pattern in C#](https://stackoverflow.com/questions/898828/finalize-dispose-pattern-in-c-sharp) – Panagiotis Kanavos Mar 28 '19 at 16:31
  • @Softlion `Dispose(true)` will never be called unless your code deliberale calls it, either through a direct call to `Dispose()` or through a `using` block to automatically dispose of an object. In all other cases, `Dispose(false)` will be called when the garbage collector calls the object's finalizer – Panagiotis Kanavos Mar 28 '19 at 16:35
  • It's NOT a duplicate, as it is a question is the SPECIAL context of Xamarin.Android objects that are both under the Java garbage collection AND the .NET garbage collection. – Softlion Mar 28 '19 at 16:36
  • @Softlion there's nothing special about Dispose. If you don't call it, it won't be called by itself. Doesn't matter whether it's Java or not. That's what the duplicates explain – Panagiotis Kanavos Mar 28 '19 at 16:37
  • Ok so i may rewrite the question as it does not answer the problem. – Softlion Mar 28 '19 at 16:37
  • @Softlion what *is* the problem? If it is that expensive resources remain alive for too long, the answer is `use a 'using' block to call Dispose automatically`. If the problem is a resource leak, it may be an incorrect implementation of `Dispose(bool)` that *doesn't* always free unmanaged resources. – Panagiotis Kanavos Mar 28 '19 at 16:39
  • Just read the questions guys. It's XAMARIN ANDROID. C# objects are backing Java objects. When java objects are recycled by java GC, the C# backed java object should get disposed correctly. It's a bug in Xamarin Android, or its something which is "normal" in Xamarin Android. – Softlion Mar 31 '19 at 12:46
  • For what it's worth, I want to back the OP on this one. @PanagiotisKanavos, you seem to have a robust and in-depth understanding of the way the disposal pattern works, and your comments are both clear and clarifying. Still, it seems there is some... heh.. monkey business going on with how the mono GC works. If nothing else the fact that it's managing the interop between a C# object interface and its backing Java object does seem to somewhat complicate things. I know at least at one point the Xamarin.Forms renderer for their Shell component was broken on its dispose... (1/2) – Bondolin Oct 28 '21 at 17:25
  • ... Having trouble finding the github issue documenting the bug, but the temporary workaround was a custom renderer that failed to call the base Dispose. I am myself currently experiencing core dumps and the like when I try to dispose an object of a class deriving from `FileObserver`. Perhaps here the answer is to "favor composition over inheritance" and just not mess with Xamarin disposal at all. Also, some docs on [Xamarin Garbage Collection](https://docs.microsoft.com/en-us/xamarin/android/internals/garbage-collection). So this should be reopened, as it does ask a unique question. (2/2) – Bondolin Oct 28 '21 at 17:32
  • P.S. - While debug stepping through my errant `Dispose` override, the specific error I am getting is `System.NotSupportedException: 'Unable to activate instance of type ... from native handle 0x771edfaf94 (key_handle 0x5df4aee).'` This looks to be the situation described by the [Disposing Other Types](https://learn.microsoft.com/en-us/xamarin/android/internals/garbage-collection#disposing-other-types) section of aforementioned documentation. – Bondolin Oct 28 '21 at 17:47

0 Answers0