Page 2 of 2

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Sat Aug 17, 2019 8:10 pm
by mk-soft

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Sat Aug 17, 2019 8:44 pm
by Mijikai
skywalk wrote:Yes, so I am asking why Mijikai says PB strings are broken?
Are you trolling?

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Sat Aug 17, 2019 9:06 pm
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.

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Sat Aug 17, 2019 9:11 pm
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!

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Sat Aug 17, 2019 9:18 pm
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)

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Sat Aug 17, 2019 9:54 pm
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?

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Sat Aug 17, 2019 11:06 pm
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.

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Wed Aug 28, 2019 12:03 pm
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
...

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Wed Aug 28, 2019 1:38 pm
by Mijikai
So basically just the constant is wrong?
Sounds like good news.

Re: Workaround FreeString Resources (Set to Nothing)

Posted: Wed Aug 28, 2019 2:51 pm
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?