Page 1 of 1
Free memory from a string used in a structure?
Posted: Sun May 10, 2009 11:49 pm
by Mistrel
How does this work? I know some people here have solved this in the past but the method eludes me.
Code: Select all
Structure That
Fred.s
EndStructure
Repeat
*This.That=AllocateMemory(SizeOf(That))
*This\Fred="Hello, Fred!"
*This\Fred=#NULL$
FreeMemory(*This)
ForEver
Posted: Mon May 11, 2009 7:47 am
by Demivec
Here's freak's solution:
Code: Select all
Procedure FreePBString(*Address)
Protected string.String ; the String Structure contains one String element, which is initialized to 0 on procedure start
PokeL(@string, *Address) ; poke our strings address into the structure
EndProcedure ; when returning, PB's string free routine for the local structure will actually free the passed string.
and here's how you would use it in your sample code:
Code: Select all
Structure That
Fred.s
EndStructure
Repeat
*This.That=AllocateMemory(SizeOf(That))
*This\Fred="Hello, Fred!"
FreePBString(@*This\Fred)
FreeMemory(*This)
ForEver
A search on freeing strings would have brought up several threads.
@Edit: added a missing "*" to the code sample. Thanks flaith and Mistrel.

Posted: Mon May 11, 2009 9:29 am
by srod
Yes I use this little routine all the time - very useful!

Posted: Mon May 11, 2009 10:36 pm
by Mistrel
Thank you. This is exactly what I was looking for.

Posted: Tue May 12, 2009 6:54 am
by flaith
Demivec wrote:
FreePBString(@This\Fred)
Maybe it should be

Posted: Tue May 12, 2009 9:01 am
by Mistrel
flaith wrote:Demivec wrote:
FreePBString(@This\Fred)
Maybe it should be

Maybe it should be

Posted: Tue May 12, 2009 10:01 am
by flaith

rigth sorry

Posted: Thu May 14, 2009 10:00 pm
by Mahan
A newbie PB question:
This only applies when allocating and managing structure-memory manually, right?
If I use structures with Strings in normal Lists (where the memory allocation is managed by PB), PB will surely handle the releasing of the internal strings when i do DeleteElement, correct?
Posted: Thu May 14, 2009 10:08 pm
by freak
Mahan wrote:If I use structures with Strings in normal Lists (where the memory allocation is managed by PB), PB will surely handle the releasing of the internal strings when i do DeleteElement, correct?
exactly.
Re: Free memory from a string used in a structure?
Posted: Thu Aug 21, 2014 10:30 am
by mariosk8s
Cool trick, but should that be PokeI or are strings addresses 32 bit on 64 platforms?
Re: Free memory from a string used in a structure?
Posted: Thu Aug 21, 2014 10:33 am
by Fred
You should use ClearStructure() instead of this trick which is not granted to work with future releases. Or better if you are in PB 5.30+: uses AllocateStructure()/FreeStructure() combo.
Re: Free memory from a string used in a structure?
Posted: Thu Aug 21, 2014 11:06 am
by mariosk8s
But i don't want to free the structure, just the string in it.
Re: Free memory from a string used in a structure?
Posted: Thu Aug 21, 2014 11:10 am
by Fred
ClearStructure() doesn't work for you ?
Re: Free memory from a string used in a structure?
Posted: Thu Aug 21, 2014 12:49 pm
by mariosk8s
Yeah, it does, but now
Code: Select all
Structure foo
int.i
string.s
endstructure
becomes
Code: Select all
Structure foo
int.i
string.String
endstructure
And
*foo\string becomes
*foo\string\s.
It maskes it a bit clunky.
But then on the other hand
Code: Select all
FreePBString(@*foo\string)
PokeI(*foo+OffsetOf(foo\string), 0)
is pretty nasty, too.