vendredi 21 septembre 2018

automatic memory release after use

There are lots of places that I got code like this:

some_function_signature() 
{
    T a;
    T b = f1(a);
    T c = f2(b);
    T d = f3(c);
    ...
}

As you can see, in such a function, a is passed to f1() to produce b, then b passed to f2() to produce c, and so on. These variables are not going to be used after the function call (f1,f2,f3...). And they hold large memory (e.g. T is big image data).

I can do something like this to free the memory after use:

some_function_signature() 
{
    T a;
    T b = f1(a); a.free();
    T c = f2(b); b.free();
    T d = f3(c); c.free();
    ...
}

I wonder if I can make this process automatic and more elegant.For example, a scoped memory management process or using sort of reference counting, but I just don't know how to best apply these methods here.

Aucun commentaire:

Enregistrer un commentaire