Deleting temporary custom types in a function

Blitz3D Forums/Blitz3D Programming/Deleting temporary custom types in a function

Fry Crayola(Posted 2005) [#1]
If I create a custom type in a function, and only need it for that function, I use Delete to get rid of it and free up that memory, yes?

What if I need to return that type? For example, a function that populates a type with data and returns the type for use. I can't use Delete before Return, and surely the Return will leave the function and so I can't execute any successive statements.

Can they be deleted, or am I stuck?


BlitzSupport(Posted 2005) [#2]
Just return it without deleting the temporary type (object). You'll be returning a pointer to the object, which gets assigned to your own pointer before the temporary pointer goes out of scope, like so:

Function CreateThing.Thing ()
    t.Thing = New Thing ; Object created here and assigned to pointer t...
    Return t ; Return value of t -- pointer to the object...
End Function

mypointer.Thing = CreateThing () ; mypointer receives pointer to object...


This returns the value of t (pointer to the object) to mypointer. The pointer t will then go out of scope as the function is exited (so it's basically non-existent), but mypointer is pointing to the object itself, so you can free the object by deleting mypointer.


Fry Crayola(Posted 2005) [#3]
Ah... so that's not a problem then.