2
votes

In Xamarin Forms I have the following class (with no content):

public class TracksBoxView : BoxView
// Uses custom renderer TracksBoxViewRenderer.cs
{
}

A ContentPage called TracksPage.cs, on a navigation stack, contains just TracksBoxView, a Label and a Switch. TracksPage generates some public data (takes a few seconds) then sets a flag that is being polled by the iOS custom renderer TracksBoxViewRenderer.cs. The custom renderer then retrieves and displays the information.

When the user navigates to TracksPage (push), the information is displayed correctly. The user then hits the 'back' button and after a few seconds navigates again to TracksPage. This is repeated. On about the second or third try, the following exception occurs:

System.ObjectDisposedException: Cannot access a disposed object.

This occurs early in the custom renderer at the following line:

double totalWidth = (double)this.Bounds.Width;

If the user delays 30 seconds or more between tries, it works most times. I imagine garbage collection is happening meanwhile.

Have not found anything similar in the forums.

Is there anything I can do about this, maybe a better way or a workaround? Does it sound like a Xamarin Forms bug?

1
I missing some context somewhere, are you saying this is the source the disposed object error? Is this TracksBoxView within a recycler? - SushiHangover
Yes @SushiHangover, the exception occurs when 'this' is referenced. My understanding is that 'this' represents the View. I have not implemented any kind of recycler. I have 100+ Labels in the View ( it's tracks on a map). - BillF
When we return to the custom renderer's view, we find that is is the very same view as previously. I would have expected a fresh view. So I have now removed any Labels in the view before adding the 100+ Labels. In case accumulation of Labels was the issue. However it did not help. - BillF
CORRECTION - it is a fresh view - my mistake on that point. - BillF
You can try overriding Dispose method in class and have a breakpoint to get the idea where it is cleaned. - Vishnu

1 Answers

1
votes

A basic error on my part. The labels were not fields. :(