locked
How to I dispose IRandomAccessStreams?

    Question

  • I am opening a bunch of files like

    file->OpenAsync(Windows::Storage::FileAccessMode::Read)).then([=](Windows::Storage::Streams::IRandomAccessStream^ stream)

    and as result, the memory usage is drastically increasing for the app.

    Does WinRT automatically clean up or do we have to manage the stream memory? If so how?

    Friday, August 03, 2012 8:27 PM

Answers

  • Nevermind.. I set the App root frame background to a background image and removed all the instances of setting the gridbackgroundimage in all the pages. That fixed it.

    Thanks!

    • Marked as answer by Kamal Lanka Saturday, August 04, 2012 1:01 AM
    Saturday, August 04, 2012 1:01 AM

All replies

  • All WinRT objects are ref-counted so when your code lets go of all references and ref-count goes to zero, the object will be destroyed. In your example however, as is for most code that calls async operations, the parameters and lambda captures will outlive the call (their ref-count will be non-zero). This is ok for most WinRT objects, but for some that contain expensive or exclusive resources, you want to be able to deterministically destroy then once you're done with them. This is possible *if* these types implement Platform::IDisposable (synonym with Windows::Foundation::IClosable).

    In your example, IRandomAccessStream is a FileRandomAccessStream. Since FileRandomAccessStream implements IDisposable/IClosable, you can call:

    delete stream;

    once you're done with the stream. This will deterministically clean any resources that are deemed expensive by the author of the WinRT type, even if its ref-count is non-zero.

    Hope this helps,
    Marian Luparu
    Visual C++


    Friday, August 03, 2012 10:47 PM
  • Thanks Marian! I tried this but it did not help much..probably I am doing something else wrong..

    Here is my scenario: I have 2 pages in my app. I am trying to set the background image of the app dynamically based on user selection from file picker.

    In both the pages I have a grid (which has its background set to imagebrush with the below code) with gridview items in them. As I navigate between these pages I see memory going from 100mb to 200mb to 300mb to more than a 1gb.

    concurrency::create_task(Common::SessionManager::BackgroundImageFile->OpenAsync(Windows::Storage::FileAccessMode::Read)).then([this](Windows::Storage::Streams::IRandomAccessStream^ stream)
    		{
    			SetBackgroundImage(stream);
    		});
    
    
    void SetBackgroundImage(Windows::Storage::Streams::IRandomAccessStream^ stream)
    {
    	BitmapImage^ backgroundBitmapImage = ref new BitmapImage();
    	backgroundBitmapImage->SetSource(stream);
    	mainPageGridBackgroundImage->ImageSource = backgroundBitmapImage;
    }

    Friday, August 03, 2012 11:15 PM
  • Nevermind.. I set the App root frame background to a background image and removed all the instances of setting the gridbackgroundimage in all the pages. That fixed it.

    Thanks!

    • Marked as answer by Kamal Lanka Saturday, August 04, 2012 1:01 AM
    Saturday, August 04, 2012 1:01 AM