Workaround FreeString Resources (Set to Nothing)

Share your advanced PureBasic knowledge/code with the community.
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Workaround FreeString Resources (Set to Nothing)

Post by mk-soft »

My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Workaround FreeString Resources (Set to Nothing)

Post by Mijikai »

skywalk wrote:Yes, so I am asking why Mijikai says PB strings are broken?
Are you trolling?
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Workaround FreeString Resources (Set to Nothing)

Post by mk-soft »

Mijikai wrote:
skywalk wrote:Yes, so I am asking why Mijikai says PB strings are broken?
Are you trolling?
No, he hadn't been involved with it before.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Workaround FreeString Resources (Set to Nothing)

Post by skywalk »

@Mijikai, Surely not. I thought you were because you trash the entire v571 for a small bug? It does not cause leaks, only inefficient with memory. I cannot wait for the release with no bugs!
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Workaround FreeString Resources (Set to Nothing)

Post by mk-soft »

Not quite...
What didn't cause any problems with PB v5.6x, leads to a memory leak with PB v5.7x.

Code: Select all

Structure sFoo
  iVal.i
  sVal.s
EndStructure

*mem.sFoo = AllocateMemory(SizeOf(sFoo))
*mem\sVal = "Hello"
;
; ...
; 
*mem\sVal = #Null$
FreeMemory(*mem)
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Workaround FreeString Resources (Set to Nothing)

Post by skywalk »

haha, but we go in circles :shock:
mk-soft wrote:If you do everything right, there will be no memory leakage
- AllocateStructure -> FreeStructure
- AllocateMemory -> ClearMemory -> FreeMemory.
You are clearing memory with a loosely documented step(#Null$). I only found it mentioned here in the manual with no examples.
manual wrote:There are two special constants for strings:
#Empty$: represents an empty string (exactly the same as "")
#Null$ : represents an null string. This can be used for API
functions requiring a null pointer to a string, or to really free a string.
Curious? Why not stick with AllocateStructure->FreeStructure to avoid this issue?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Workaround FreeString Resources (Set to Nothing)

Post by mk-soft »

skywalk wrote: Curious? Why not stick with AllocateStructure->FreeStructure to avoid this issue?
Depends on the task if you need it that way.
It is not always possible to work with AllocateStructure or to rebuild the whole structure every time only to set a string to Nothing for API calls.

It should work as described in the description.
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: Workaround FreeString Resources (Set to Nothing)

Post by mk-soft »

Josh Link: viewtopic.php?f=4&t=71568&start=15#p541135

Set String to Nothing :wink:

Code: Select all

Macro Nothing
  "!_PB_NullConstant_!"
EndMacro

Define s.s

s = "Global Hello World"
Debug s
Debug "Adress to String = " + @s
s = #Null$
Debug "Adress to String (#Null$) = " + @s ; <- Must be 0.

s = Nothing
Debug "Adress to String (Nothing) = " + @s ; <- Must be 0.

Structure udtFoo
  iVal.i
  sVal.s
EndStructure

a.udtFoo
a\iVal = 100
a\sVal = "Structure Hello World"
Debug a\sVal
Debug "Adress to String = " + PeekI(@a+OffsetOf(udtFoo\sVal))
Debug a\sVal

a\sVal = Nothing
Debug "Adress to String (Nothing) = " + PeekI(@a+OffsetOf(udtFoo\sVal))
Debug a\sVal
ASM-Code is ok
...
; s = Nothing
XOR rsi,rsi
LEA rdi,[v_s]
CALL _SYS_FastAllocateStringFree4
...
; a\sVal = Nothing
XOR rsi,rsi
LEA rdi,[rbp+8]
CALL _SYS_FastAllocateStringFree4
...
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
User avatar
Mijikai
Addict
Addict
Posts: 1360
Joined: Sun Sep 11, 2016 2:17 pm

Re: Workaround FreeString Resources (Set to Nothing)

Post by Mijikai »

So basically just the constant is wrong?
Sounds like good news.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: Workaround FreeString Resources (Set to Nothing)

Post by skywalk »

This is why I hate this approach.
Just make a function that clears strings and be done with this. :evil:

Code: Select all

EnableExplicit
#Null$ = "!_PB_NullConstant_!"
Debug #Null$    ;BAD Design if constant has hidden meaning?
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Post Reply