Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Posts: 3415 Joined: Sat Jun 30, 2007 8:04 pm
Post
by Mistrel » Sun May 10, 2009 11:49 pm
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
Demivec
Addict
Posts: 4260 Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA
Post
by Demivec » Mon May 11, 2009 7:47 am
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.
Last edited by
Demivec on Tue May 12, 2009 1:58 pm, edited 2 times in total.
srod
PureBasic Expert
Posts: 10589 Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...
Post
by srod » Mon May 11, 2009 9:29 am
Yes I use this little routine all the time - very useful!
I may look like a mule, but I'm not a complete ass.
Mistrel
Addict
Posts: 3415 Joined: Sat Jun 30, 2007 8:04 pm
Post
by Mistrel » Mon May 11, 2009 10:36 pm
Thank you. This is exactly what I was looking for.
flaith
Enthusiast
Posts: 704 Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:
Post
by flaith » Tue May 12, 2009 6:54 am
Demivec wrote:
FreePBString(@This\Fred)
Maybe it should be
“Fear is a reaction. Courage is a decision.” - WC
Mistrel
Addict
Posts: 3415 Joined: Sat Jun 30, 2007 8:04 pm
Post
by Mistrel » Tue May 12, 2009 9:01 am
flaith wrote: Demivec wrote:
FreePBString(@This\Fred)
Maybe it should be
Maybe it should be
flaith
Enthusiast
Posts: 704 Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:
Post
by flaith » Tue May 12, 2009 10:01 am
rigth sorry
“Fear is a reaction. Courage is a decision.” - WC
Mahan
User
Posts: 35 Joined: Sun Jan 25, 2009 10:12 am
Location: Sweden
Post
by Mahan » Thu May 14, 2009 10:00 pm
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?
freak
PureBasic Team
Posts: 5940 Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany
Post
by freak » Thu May 14, 2009 10:08 pm
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.
quidquid Latine dictum sit altum videtur
mariosk8s
Enthusiast
Posts: 103 Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:
Post
by mariosk8s » Thu Aug 21, 2014 10:30 am
Cool trick, but should that be PokeI or are strings addresses 32 bit on 64 platforms?
Fred
Administrator
Posts: 18162 Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:
Post
by Fred » Thu Aug 21, 2014 10:33 am
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.
mariosk8s
Enthusiast
Posts: 103 Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:
Post
by mariosk8s » Thu Aug 21, 2014 11:06 am
But i don't want to free the structure, just the string in it.
Fred
Administrator
Posts: 18162 Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:
Post
by Fred » Thu Aug 21, 2014 11:10 am
ClearStructure() doesn't work for you ?
mariosk8s
Enthusiast
Posts: 103 Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:
Post
by mariosk8s » Thu Aug 21, 2014 12:49 pm
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.