PureBasic Forum
https://www.purebasic.fr/english/

Workaround FreeString Resources (Set to Nothing)
https://www.purebasic.fr/english/viewtopic.php?f=12&t=73418
Page 2 of 2

Author:  mk-soft [ Sat Aug 17, 2019 8:10 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

Link to bug-reports
viewtopic.php?f=4&t=71568&start=0#p531589
viewtopic.php?f=4&t=71568&start=15#p538347

Author:  Mijikai [ Sat Aug 17, 2019 8:44 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

skywalk wrote:
Yes, so I am asking why Mijikai says PB strings are broken?


Are you trolling?

Author:  mk-soft [ Sat Aug 17, 2019 9:06 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

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.

Author:  skywalk [ Sat Aug 17, 2019 9:11 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

@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!

Author:  mk-soft [ Sat Aug 17, 2019 9:18 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

Not quite...
What didn't cause any problems with PB v5.6x, leads to a memory leak with PB v5.7x.
Code:
Structure sFoo
  iVal.i
  sVal.s
EndStructure

*mem.sFoo = AllocateMemory(SizeOf(sFoo))
*mem\sVal = "Hello"
;
; ...
;
*mem\sVal = #Null$
FreeMemory(*mem)

Author:  skywalk [ Sat Aug 17, 2019 9:54 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

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?

Author:  mk-soft [ Sat Aug 17, 2019 11:06 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

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.

Author:  mk-soft [ Wed Aug 28, 2019 12:03 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

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

Set String to Nothing :wink:
Code:
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
Quote:
...
; 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
...

Author:  Mijikai [ Wed Aug 28, 2019 1:38 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

So basically just the constant is wrong?
Sounds like good news.

Author:  skywalk [ Wed Aug 28, 2019 2:51 pm ]
Post subject:  Re: Workaround FreeString Resources (Set to Nothing)

This is why I hate this approach.
Just make a function that clears strings and be done with this. :evil:
Code:
EnableExplicit
#Null$ = "!_PB_NullConstant_!"
Debug #Null$    ;BAD Design if constant has hidden meaning?

Page 2 of 2 All times are UTC + 1 hour
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
http://www.phpbb.com/