Page 1 of 2

[Implemented] FreeStructureStrings as normal PB FUNCTION

Posted: Sat Mar 18, 2006 1:56 am
by helpy
There is still a memory leak when using dynamic Structures with strings.

If a allocate Memory for Structures, which contains strings and then free the memory again ... the memory of the strings are still allocated.

Check this code:

Code: Select all

Macro PAUSE(MSG = "Check Memory in Taskmanager!")
  MessageRequester("WAIT",MSG)
EndMacro

#TestSize = 1024*1024

Structure StructWithStrings
  s1.s
  x.l
  s2.s
EndStructure

Global Dim aPointers(#TestSize)
*PStruct.StructWithStrings

PAUSE("Array allocated!")

For i = 0 To #TestSize
  aPointers(i) = AllocateMemory(SizeOf(StructWithStrings))
Next i

PAUSE("Memory for Structures allocated")

For i = 0 To #TestSize
  *PStruct = aPointers(i)
  *PStruct\s1 = "test s1: " + Str(i)
  *PStruct\s2 = "test s2: " + Str(i)
Next i

PAUSE("Structures filled With strings")

For i = 0 To #TestSize
  FreeMemory(aPointers(i))
Next i

PAUSE("Memory free again??? NO!!!")
There is a intern purebasic function (_SYS_FreeStructureStrings@8) for freeing structures with strings.

Would it be possible to make this intern function open, so I can use a PB-function to free strings in structures.

Example:

Code: Select all

; ... code see previous example

PAUSE("Memory for Structures allocated")

For i = 0 To #TestSize
  *PStruct = aPointers(i)
  *PStruct\s1 = "test s1: " + Str(i)
  *PStruct\s2 = "test s2: " + Str(i)
Next i

PAUSE("Structures filled With strings")

For i = 0 To #TestSize
  FreeStructureStrings(*PStruct)   ; <<<< NEW PB FUNCTION
  FreeMemory(aPointers(i))
Next i

PAUSE("Memory free again??? NO!!!")
@Fred:
Is this possible????

cu, helpy

Posted: Sat Mar 18, 2006 6:14 pm
by helpy
Would somone else need this possibility?

Posted: Sat Mar 18, 2006 6:15 pm
by Killswitch
It'd be quite handy for me!

Posted: Sat Mar 18, 2006 6:22 pm
by freak
Maybe if you would not write your stuff in such an annoying big red font, we might even pay attention...

Posted: Sat Mar 18, 2006 6:27 pm
by helpy
Thank you, freak

OK! I changed it and I will take it to heart!

cu, helpy

Posted: Sat Mar 18, 2006 7:26 pm
by Trond
freak wrote:Maybe if you would not write your stuff in such an annoying big red font, we might even pay attention...
This "attention" guy must be really poor, no one ever pays him.

Posted: Sun Mar 19, 2006 1:23 pm
by Fred
Actually it could be a good idea for dynamic structures, i put it on the now famous TODO list.

Posted: Sun Mar 19, 2006 2:02 pm
by MrMat
This will be useful. Thanks.

Posted: Sun Mar 19, 2006 2:21 pm
by helpy
Yes! Very useful!

Thank you Fred :-)

Posted: Sun Mar 19, 2006 2:22 pm
by Dare2
Trond wrote:This "attention" guy must be really poor, no one ever pays him.
LOL. :lol:

Posted: Sun Mar 19, 2006 2:29 pm
by helpy
Dare2 wrote:
Trond wrote:This "attention" guy must be really poor, no one ever pays him.
LOL. :lol:
OK! OK! :oops: ... In future I will pay attention to the way how I express things ;-)

But this feature will be really helpful to finish "OOP with macros and functions from an include file"

cu, helpy

Posted: Sun Mar 19, 2006 2:31 pm
by Dare2
Hehe, I don't think that was directed your way helpy. :) Just a very funny remark taking advantage of the way posts panned out.

BTW, Freak, need a valium ...? :)

Re: FreeStructureStrings as normal PB FUNCTION/COMMAND

Posted: Wed Mar 22, 2006 11:52 am
by traumatic
helpy wrote:There is a intern purebasic function (_SYS_FreeStructureStrings@8) for freeing structures with strings.

Would it be possible to make this intern function open, so I can use a PB-function to free strings in structures.

Code: Select all

Import "systembase.lib"
  SYS_FreeStructureStrings(a,b)
EndImport
;)

Re: FreeStructureStrings as normal PB FUNCTION/COMMAND

Posted: Wed Mar 22, 2006 12:19 pm
by helpy
traumatic wrote:
helpy wrote:There is a intern purebasic function (_SYS_FreeStructureStrings@8) for freeing structures with strings.

Would it be possible to make this intern function open, so I can use a PB-function to free strings in structures.

Code: Select all

Import "systembase.lib"
  SYS_FreeStructureStrings(a,b)
EndImport
;)
The two arguments (a,b):
  • a pointer to the structure
  • a pointer to a data table, which gives the information where in the structure are strings.
Example of two structures:

Code: Select all

Structure StructWithStrings
  s1.s
  x.l
  s2.s
EndStructure

Structure AnotherStructWithStrings
  s1.s
  x.l
  s2.s
  st.StructWithStrings
  s3.s
EndStructure
The Compiler creates the following data tables (in the ASM output):

Code: Select all

_SYS_StaticStringEnd:
align 4
align 4
s_s:
  dd     0
  dd     -1
s_structwithstrings:
  dd     0
  dd     8
  dd     -1
s_anotherstructwithstrings:
  dd     0
  dd     8
  dd     -2, 1, 12, 12, s_structwithstrings
  dd     24
  dd     -1
align 4
In order to free the strings in a dynamic structure *test.AnotherStructWithStrings, I should pass a pointer to s_anotherstructwithstrings.

How could I do this? If the Compiler itself creates this data table, I do not want to create it by my own in the PB source code.

cu, helpy

[sorry]
I first wrote in my native language ... changed it now
[/sorry]

Re: FreeStructureStrings as normal PB FUNCTION/COMMAND

Posted: Wed Mar 22, 2006 12:33 pm
by traumatic
...und wenn Du Deine Frage in Englisch stellen würdest, gäb's vielleicht auch
eine größere Chance auf eine Antwort...


I don't know, I just wanted to point out that it may be possible already.

As you know what the params should look like and you also know where
in your structures you're using strings, it's not impossible at least, is it?
Right now, I can't think of a way to retrieve the needed values from the
generated code though.

Maybe describing the problem in english again will help to get answers
by others.