How to Free Memory without causing a memory leak

Just starting out? Need help? Post your questions and find answers here.
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: How to Free Memory without causing a memory leak

Post by Danilo »

@StarBootics:
You just need to add ClearStructure() in your original example (1st posting):

Code: Select all

*Ptr\s = "November 18th, 2019"
ClearStructure(*Ptr,String)
FreeMemory(Memory)
Debug *Ptr\s ; *Ptr is invalid
You use the memory as "Structure String", so you need to use ClearStructure with type "String".

Using "ShowMemoryViewer():CallDebugger" you can look at the memory area:

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : Question
; File Name : How to Free Memory.pb
; File version: 1.0.0
; Programming : OK
; Programmed by : StarBootics
; Date : 18-11-2019
; Last Update : 18-11-2019
; PureBasic code : V5.71 LTS
; Platform : Windows, Linux, MacOS X
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

Memory = AllocateMemory(3*SizeOf(String))
ShowMemoryViewer(Memory, 3*SizeOf(String)):CallDebugger
*Ptr.String = Memory

*Ptr\s = "PureBasic"
ShowMemoryViewer(Memory, 3*SizeOf(String)):CallDebugger
ClearStructure(*Ptr,String)
ShowMemoryViewer(Memory, 3*SizeOf(String)):CallDebugger
*Ptr + SizeOf(String)

*Ptr\s = "Version 5.71 LTS x64"
ShowMemoryViewer(Memory, 3*SizeOf(String)):CallDebugger
ClearStructure(*Ptr,String)
ShowMemoryViewer(Memory, 3*SizeOf(String)):CallDebugger
*Ptr + SizeOf(String)

*Ptr\s = "November 18th, 2019"
ShowMemoryViewer(Memory, 3*SizeOf(String)):CallDebugger
ClearStructure(*Ptr,String)
ShowMemoryViewer(Memory, 3*SizeOf(String)):CallDebugger

FreeMemory(Memory)

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
Switch to "Hex View" in the memory viewer and step through the program by using debugger: Continue (F7)
Post Reply