Page 1 of 1

Freeing strings, values, dims etc.

Posted: Sun Mar 18, 2007 1:41 pm
by CadeX
For reinitialization purposes i'm trying to make sure i can completely empty the memory of the program.

Basically, for example:

Code: Select all

String.s="Hello!"
Dim Hi(2)
Hi(1)=3
Debug String
Debug Hi(1)
;Free "String" Memory
;Free "Hi"
Debug String ;Should be 0
Debug Hi(1) ;Should get an error saying the memory doesn't exist.
I've tried

Code: Select all

FreeMemory(@String)
which obviously hasn't worked.

Could someone help fill in the blanks? Thanks for reading.

P.S - Sorry if my post makes no sense, i try my best to explain what i'm after.

Posted: Sun Mar 18, 2007 1:56 pm
by Kaeru Gaman
you cannot destroy simple variables despite by a newstart...
arrays you can free by using

Code: Select all

Dim a(0)

Posted: Sun Mar 18, 2007 2:01 pm
by CadeX
So, doing:

Code: Select all

String.s = "Hi"
and then

Code: Select all

String = ""
Would somewhat free it?

Posted: Sun Mar 18, 2007 2:04 pm
by Kaeru Gaman
maybe it FREEs the mem used for the string, but it would not free the pointer....

remember that string-vars in PB are pointers to MEM where the string data risides.
so if you declare

Code: Select all

String.s = "" 
I presume that "String" will hold a pointer to a one-byte-mem that holds an ending 0.

Posted: Sun Mar 18, 2007 2:08 pm
by CadeX
That's no good then, i need to free the pointer AND the value.

Posted: Sun Mar 18, 2007 2:10 pm
by Kaeru Gaman
as far I know there is no native possibility in PB to free simple variables...

is there in C?

anyhow.. what you need it for?

Posted: Sun Mar 18, 2007 2:15 pm
by CadeX
I'm writing an engine to the best of my ability. It struck me when i came into the posibility of re-initialising the engine without having to restart the entire process. I then came to the point where i may not need the program to use the engine after a certain point... That's when i thought it would be helpful to clear the memory. Although each value uses little to no memory, i can live with it if there's no, "simple", way.

Posted: Sun Mar 18, 2007 2:27 pm
by Kaeru Gaman
yeah...
if you want to do something that way, just rely on allocating memory "manually" and asigning it to your own pointers...

surely this is additional efford...

perhaps it may help if you carry all your values not in simple variables
but use an array you can easily free and some constants to address them...

Posted: Sun Mar 18, 2007 4:21 pm
by Psychophanta
CadeX, may be this is useful:
http://www.purebasic.fr/english/viewtopic.php?t=23099
look at the solution posted by Freak

Posted: Sun Mar 18, 2007 9:39 pm
by Trond
CadeX wrote:I'm writing an engine to the best of my ability. It struck me when i came into the posibility of re-initialising the engine without having to restart the entire process. I then came to the point where i may not need the program to use the engine after a certain point... That's when i thought it would be helpful to clear the memory. Although each value uses little to no memory, i can live with it if there's no, "simple", way.
Then you definetely don't want to free the pointer itself, you want to reset it to its initial value.