[Implemented] FreeStructureStrings as normal PB FUNCTION

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

[Implemented] FreeStructureStrings as normal PB FUNCTION

Post 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
Last edited by helpy on Sat Mar 18, 2006 6:25 pm, edited 1 time in total.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

Would somone else need this possibility?
Killswitch
Enthusiast
Enthusiast
Posts: 731
Joined: Wed Apr 21, 2004 7:12 pm

Post by Killswitch »

It'd be quite handy for me!
~I see one problem with your reasoning: the fact is thats not a chicken~
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

Maybe if you would not write your stuff in such an annoying big red font, we might even pay attention...
quidquid Latine dictum sit altum videtur
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

Thank you, freak

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

cu, helpy
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Post by Fred »

Actually it could be a good idea for dynamic structures, i put it on the now famous TODO list.
MrMat
Enthusiast
Enthusiast
Posts: 762
Joined: Sun Sep 05, 2004 6:27 am
Location: England

Post by MrMat »

This will be useful. Thanks.
Mat
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post by helpy »

Yes! Very useful!

Thank you Fred :-)
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post by Dare2 »

Trond wrote:This "attention" guy must be really poor, no one ever pays him.
LOL. :lol:
@}--`--,-- A rose by any other name ..
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Post 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
Dare2
Moderator
Moderator
Posts: 3321
Joined: Sat Dec 27, 2003 3:55 am
Location: Great Southern Land

Post 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 ...? :)
@}--`--,-- A rose by any other name ..
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: FreeStructureStrings as normal PB FUNCTION/COMMAND

Post 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
;)
Good programmers don't comment their code. It was hard to write, should be hard to read.
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: FreeStructureStrings as normal PB FUNCTION/COMMAND

Post 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]
Last edited by helpy on Wed Mar 22, 2006 12:37 pm, edited 1 time in total.
traumatic
PureBasic Expert
PureBasic Expert
Posts: 1661
Joined: Sun Apr 27, 2003 4:41 pm
Location: Germany
Contact:

Re: FreeStructureStrings as normal PB FUNCTION/COMMAND

Post 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.
Good programmers don't comment their code. It was hard to write, should be hard to read.
Post Reply