Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

New() resurrecting old disposed instances (crash)

$
0
0

Hi there

I'm having a problem that I'm not sure how to fix. When my android app starts, I create a new object, and when the app ends I Dispose() the object and set it to null. The next time I start, allocating a new object seems to always use the old disposed object and causes a crash.

Some example code (from the MainActivity.cs file):

private ShelterGame g = null;

protected override void OnResume ()
{
    if (g == null) {
        ShelterGame.Activity = this;
        g = new ShelterGame ();
        SetContentView (g.Window);
        g.Run ();
    }

    base.OnResume ();
}

protected override void OnDestroy ()
{
    base.OnDestroy ();
    g.Dispose ();
    g = null;
    GC.Collect (GC.MaxGeneration);
}

On every second run of the app, the variable g uses the same ShelterGame object from the previous run which has been disposed and is therefore invalid. I have added breakpoints and know that the correct code is hit at the correct intervals (and on the second run, new ShelterGame(); is definitely called.

Is there a way for me to prevent this and ensure that on every run of the app, the disposed object is fully collected and a genuinely new object is allocated and constructed?

On a side note, exiting my app (A MonoGame game) does not seem to detach the debugger. Is there something that could cause this that might be related?

Thanks

Chris


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>