Object Disposal after/before/during Return?

classic Classic list List threaded Threaded
3 messages Options
jrd
Reply | Threaded
Open this post in threaded view
|

Object Disposal after/before/during Return?

jrd
In general, what's the best way of disposing/handling memory deallocation of an object in the context of needing to return it specifically for a function/method?

If you create a local object in a given function/method (e.g., an Array) and you need to return this object as the result of that function/method, you either would have to...

function Array Foo()
{
    var Array result;

    let result = Array.new(10);

    ....

    return result; // How to dispose?
}


1) Dispose of the Array result BEFORE returning it, thereby not having it available to return as the result of the function...

or...

2) Return the Array FIRST as the result of called function/method, but then you have no further access to it for dispose()/deallocation in the original calling function (unless it was created as a static object and not a local object).  That seems unlikely since you'd never create it as a temporary local object in the first place.

This must come up alot in general programming procedure.  

Any thoughts how to best handle this memory deallocation procedure?

- JRD
Reply | Threaded
Open this post in threaded view
|

Re: Object Disposal after/before/during Return?

ybakos
Given:
function Array Foo() { 
    var Array result; 
    let result = Array.new(10); 
    ...
    return result; // How to dispose? 
}

Know that 10 addresses in RAM have been allocated in the heap, and the base (first) address is assigned to result. Function Foo returns a copy of result. It would then be the caller's responsibility to "destroy the object," thereby deallocating the Array in memory. For example:

var myArray = Foo();
// do what you need to with myArray
myArray.dispose();
Reply | Threaded
Open this post in threaded view
|

Re: Object Disposal after/before/during Return?

cadet1620
Administrator
In reply to this post by jrd
jrd wrote
In general, what's the best way of disposing/handling memory deallocation of an object in the context of needing to return it specifically for a function/method?
When a function returns an object that it created, it passes ownership of that object to the calling function.  The calling function must either dispose the object or return it.

class Whatever
{
    function Array Foo()
    {
        var Array result;

        let result = Array.new(10);
        ....

        return result;    // Calling function now owns the object.
    }
}

class Something
{
    function void Bar()
    {
        var Array thing;

        let thing = Whatever.Foo();    // Returns new Array object
        ....

        do thing.dispose();
        return;
    }
}

Sometimes you want to do this in the opposite direction; give an object that you created to a container object, and let the container take care of disposing what you put in it.

function void stash(Box box)
{
    var Thing foo;
    do box.add(foo);    // box now owns foo -- we can no longer touch it.
    let foo = 0;
    ...
    return;
}

function void trash(Box box)
{
    do box.empty();    // disposes all Things in box
    return;
}

--Mark