assign var from local to global without duplicate in memory (makeImageRequest)

Hi, 

I am loading image via makeImageRequest. I get data via responseCallback which saves BitmapResource to its parameter. Is there way to save this data to my global var without having data twice in the memory?

Thanks

  • I can't see a way to get around this.  When the callback is called, it's a parameter so on the call stack, and if want to use it, you need to save it someplace.  The copy on the call stack will go away when you return from the callback.

  • MonkeyC uses object handles and reference counting when assigning variables. Consider the following code:

    var globalImageResourceRef = null;
    
    function onImageResponse(responseCode, imageResource)
    {
        if (responseCode == 200) {
            globalImageResourceRef = imageResource;
        }
    }
    

    On line 6, the handle `globalImageResourceRef` is associated with the same data as the handle `imageResource` that was passed in by the system. No additional copy of the data is made, we simply increment the reference count by one to account for the new reference to the data. After line 6, the reference count on that data is 2. When the code that called `onImageResponse` cleans up, it will decrement the reference count to that data down to one (`globalImageResourceRef` is the only valid handle to that memory now).

    If you don't want to keep the image in memory after the calling code does its cleanup, you can store to Application.Storage using a call like this:

    // using Application.Storage;
    
    Storage.setValue(999, imageResource);

    When you want to retrieve the value later, you can get it back like any other data in Application.Storage:

    var imageResource = Storage.getValue(999);
    if (imageResource != null) {
        // be sure we had a value stored
    }

  • Interesting Travis!  Old dogs like me can learn new tricks!  Slight smile

  • Thanks Travis, you are indeed correct. It seems that in my case duplicate in memory was caused by not cleaning variable before inserting another object with the same size. When I set it to null first, I am able to save precious memory. 

  • Yes. That is actually a pretty common issue to run into. I'm sorry I forgot to mention it.