is this healthy?

BlitzPlus Forums/BlitzPlus Programming/is this healthy?

CS_TBL(Posted 2004) [#1]
n00bish question perhaps ..
Notify"start"
For t=0 To 255
	bla=CreateBank(1024)
	FreeBank bla 
Next
Notify"end"
End


It's not about this being anything that makes sense, but I can imagine myself using tempbuffers etc.. same for CreateImage and FreeImage. Are the resources really gone after I freed them, or are they silently preserved somewhere (while not being useable for me) and thus slowly eating my mem until the space-time-continuum is messed-up and the universe collapses?


Mr Brine(Posted 2004) [#2]
I wouldnt worry to much about the universe collapsing!!


aab(Posted 2004) [#3]
Quote:
"n00bish question perhaps .."
I dont think it is.
More of an elaborate question to me.
I assume that freeing the memory frees the memory.. But to what extent is beyond my knowledge


soja(Posted 2004) [#4]
If I were you, I wouldn't worry about it. Just assume that FreeBank does what it's supposed to do. It's probable that some of the memory remains allocated to Blitz, but I'm positive that it's marked for other use once you FreeBank it, and there's probably a cap on that memory. I'm sure if there were memory leaks, they would have been caught by now. =)


CS_TBL(Posted 2004) [#5]
Banks and images created in a function are also really deleted at the end of a function? No need to FreeImage/FreeBank them in the actual function?


QuietBloke(Posted 2004) [#6]
no.. they exist until either you delete them or the program ends at which point blitz will do a clean up.

eg.

createbank ()

; Do stuff

end

function createbank ()
blah = CreateBank (1024 )
end function

This code will call the function which will create the bank and assign the local variable blah to point to it.

at the end of the function the blah variable will cease to exist but the bank you reated is still there.. you just no longer have a way to access it.

When the program ends Blitz will do a clean up and delete the bank.

Hope that helps ( and I hope Im right ).


CS_TBL(Posted 2004) [#7]
Hm.. funny, isn't it? everything in a function is local and thus deleted at the end of a function, except the contents from created banks, images etc.

How's this (gonna be) in blitzmax? Personally I find that non-local stuff in a local scope a bit weird.. I really expect *everything* to be gone to the dogs, once a function ends..

Or do you ppl find this all normal ? :)


soja(Posted 2004) [#8]
don't forget types...

It's normal if you've ever programmed in C/C++, at least.

Once you allocate memory (with malloc or New) you must explicitly free it. You can create a local pointer to it, but that's seperate from the actual allocated memory. If that pointer is created within a function, then it's destroyed when program execution leaves that function's scope. The allocated memory persists, which is good, because if it didn't, you'd have to use global variables or always pass in a pointer to a memory bank for the function to fill or something.

Blitz does a very, very similar thing.

Don't think of chunks of memory (type instances, memory banks, images, etc) as "variables". For these types of things, the variables are the pointers to the object, and the variables are what get destroyed when you leave scope -- not the memory blocks.