Free memory from a string used in a structure?

Just starting out? Need help? Post your questions and find answers here.
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Free memory from a string used in a structure?

Post 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
User avatar
Demivec
Addict
Addict
Posts: 4260
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Post 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. :wink:
Last edited by Demivec on Tue May 12, 2009 1:58 pm, edited 2 times in total.
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post by srod »

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
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

Thank you. This is exactly what I was looking for. :)
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

Demivec wrote: FreePBString(@This\Fred)
Maybe it should be

Code: Select all

FreePBString(*@This\Fred)
:?:
“Fear is a reaction. Courage is a decision.” - WC
Mistrel
Addict
Addict
Posts: 3415
Joined: Sat Jun 30, 2007 8:04 pm

Post by Mistrel »

flaith wrote:
Demivec wrote: FreePBString(@This\Fred)
Maybe it should be

Code: Select all

FreePBString(*@This\Fred)
:?:
Maybe it should be

Code: Select all

FreePBString(@*This\Fred)
:)
User avatar
flaith
Enthusiast
Enthusiast
Posts: 704
Joined: Mon Apr 25, 2005 9:28 pm
Location: $300:20 58 FC 60 - Rennes
Contact:

Post by flaith »

:oops: rigth sorry :lol:
“Fear is a reaction. Courage is a decision.” - WC
Mahan
User
User
Posts: 35
Joined: Sun Jan 25, 2009 10:12 am
Location: Sweden

Post 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?
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post 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.
quidquid Latine dictum sit altum videtur
User avatar
mariosk8s
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:

Re: Free memory from a string used in a structure?

Post by mariosk8s »

Cool trick, but should that be PokeI or are strings addresses 32 bit on 64 platforms?
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Free memory from a string used in a structure?

Post 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.
User avatar
mariosk8s
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:

Re: Free memory from a string used in a structure?

Post by mariosk8s »

But i don't want to free the structure, just the string in it.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Free memory from a string used in a structure?

Post by Fred »

ClearStructure() doesn't work for you ?
User avatar
mariosk8s
Enthusiast
Enthusiast
Posts: 103
Joined: Wed Apr 06, 2011 11:37 am
Location: Hüfingen, Germany
Contact:

Re: Free memory from a string used in a structure?

Post 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.
Post Reply