Implement Dispose and Finalize for Windows Silverlight Pages Phone

I have a great solution with graphics, pop-ups and animations. I found that while navigating the pages, I had a massive memory leak.

Is trying

So I tried with the first solution:

protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        if (App.RootFrame.CanGoBack)
            App.RootFrame.RemoveBackEntry();
        GC.Collect();
        base.OnNavigatedTo(e);
    }

From several sources in MSDN and Stackoverflow, the memory saved for this page should be deleted. This was not so, and I am not sure that the structure of the MVVM code somehow stores the information. Then I tried to implement deconstructors and force values ​​equal to zero when the event was fired as follows:

~SecondScreen()
    {
        In_Game_Crest = null;
        currentViewModel = null;
    }

, . , . , IDisposable fiddeling viewmodelLocator, MVVMLight, - .

:  qaru.site/questions/1569714/...

/ #

MSDN:

MSDN: Finalize Dispose

, . Windows?

MVVM, ViewModel ?

Windows .


Dispose

, , , ? . , MSDN , :

private bool disposed = false;

    public void Dispose()
    {
        Dispose(true);
        // Take yourself off the Finalization queue 
        // to prevent finalization code for this object
        // from executing a second time.
        GC.SuppressFinalize(this);
    }

    protected virtual void Dispose(bool disposing)
    {
  // Check to see if Dispose has already been called.
  if(!this.disposed)
  {
     // If disposing equals true, dispose all managed 
     // and unmanaged resources.
     if(disposing)
     {
        // Dispose managed resources.
         currentView = null;
         popup = null;
         Image1 = null;
         Image2 = null;
     }
          // Release unmanaged resources. If disposing is false, 
          // only the following code is executed.
          this.Content = null;
          // Note that this is not thread safe.
          // Another thread could start disposing the object
          // after the managed resources are disposed,
          // but before the disposed flag is set to true.
          // If thread safety is necessary, it must be
          // implemented by the client.

  }
  disposed = true;         
    }

    // Use C# destructor syntax for finalization code.
    // This destructor will run only if the Dispose method 
    // does not get called.
    // It gives your base class the opportunity to finalize.
    // Do not provide destructors in types derived from this class.

    ~FirstPage()
    {
        Dispose(false);

    }
    protected override void OnNavigatedFrom(NavigationEventArgs e)
    {
        this.Dispose();
        base.OnNavigatedFrom(e);
    }

, 23 , . , , ?

this = null, base.Dispose()

, this = null base.Dispose(). , , IDisposable? ? , ?

Microsoft Profiler

, FirstPage enter image description here , . . : enter image description here , FirstPage . , ? .

+4
2

, , :

Windows-

- , , . - , .

0

, ,
, - , .
vis.a.vis, .
, . , , , , .
PageBase NavigationHelper NavigationService . , FirstPage , .
, ; , .
, .
, Dispose.
Dispose - , GarbageCollector, Dispose - , , , , , .
, , , null . null , . . .

//Call on OnClosing or OnExit or similar context
protected override void Dispose(bool isDisposing)
{

        if(isDisposing && !_isDisposed){
         if(disposeableImage != null){
           disposeableImage.Dispose();
           disposeableImage = null;
         }
        }
}
+1

Source: https://habr.com/ru/post/1569712/


All Articles