XML Memory leak

Just starting out? Need help? Post your questions and find answers here.
User avatar
StarBootics
Addict
Addict
Posts: 1006
Joined: Sun Jul 07, 2013 11:35 am
Location: Canada

XML Memory leak

Post by StarBootics »

Hello everyone,

A small XML example coming from the Help file when compiled with the ASM backend and run with valgrind we have 50 allocs but only 48 frees.
With the C backend we also have 50 allocs but only 47 frees.

A typical valgrind report :
valgrind --leak-check=full --show-leak-kinds=all ./XMLTest
==6939== Memcheck, a memory error detector
==6939== Copyright (C) 2002-2022, and GNU GPL'd, by Julian Seward et al.
==6939== Using Valgrind-3.19.0 and LibVEX; rerun with -h for copyright info
==6939== Command: ./XMLTest
==6939==
==6939==
==6939== HEAP SUMMARY:
==6939== in use at exit: 136 bytes in 2 blocks
==6939== total heap usage: 50 allocs, 48 frees, 16,468 bytes allocated
==6939==
==6939== 64 bytes in 1 blocks are still reachable in loss record 1 of 2
==6939== at 0x48455EF: calloc (vg_replace_malloc.c:1328)
==6939== by 0x421DA1: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939== by 0x421E60: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939== by 0x4060E0: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939== by 0x403D9D: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939== by 0x40303A: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939==
==6939== 72 bytes in 1 blocks are still reachable in loss record 2 of 2
==6939== at 0x48455EF: calloc (vg_replace_malloc.c:1328)
==6939== by 0x421DA1: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939== by 0x421E60: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939== by 0x406106: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939== by 0x403D9D: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939== by 0x40303A: ??? (in /home/starbootics/MemLeak/XMLTest)
==6939==
==6939== LEAK SUMMARY:
==6939== definitely lost: 0 bytes in 0 blocks
==6939== indirectly lost: 0 bytes in 0 blocks
==6939== possibly lost: 0 bytes in 0 blocks
==6939== still reachable: 136 bytes in 2 blocks
==6939== of which reachable via heuristic:
==6939== length64 : 136 bytes in 2 blocks
==6939== suppressed: 0 bytes in 0 blocks
==6939==
==6939== For lists of detected and suppressed errors, rerun with: -s
==6939== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

Code: Select all

; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Project name : XMLTest
; File Name : XMLTest.pb
; File version: 1.0.0
; Programming : Bug Demonstrator
; Programmed by : StarBootics
; Date : March 14th, 2024
; Last Update : March 14th, 2024
; PureBasic code : V6.10 LTS beta 8
; Platform : Debian GNU/Linux 12 (bookworm) x86-64
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
; Memory leak problem visible using valgrind.
;
; ASM Backend 50 allocs 48 frees
; C Backend 50 allocs 47 frees
;
; My guess we should have the same amount of 
; frees than we have allocs to avoid memory 
; leaks.
;
; The example came for the help, I only added
; the FreeXML() instruction.
;
; <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

; Create xml tree
xml = CreateXML(#PB_Any) 
mainNode = CreateXMLNode(RootXMLNode(xml), "Zoo") 

; Create first xml node (in main node)
item = CreateXMLNode(mainNode, "Animal") 
SetXMLAttribute(item, "id", "1") 
SetXMLNodeText(item, "Elephant") 

; Create second xml node (in main node)
item = CreateXMLNode(mainNode, "Animal") 
SetXMLAttribute(item, "id", "2") 
SetXMLNodeText(item, "Tiger") 

; Save the xml tree into a xml file
SaveXML(xml, "demo.xml")

; Free the xml tree
FreeXML(xml)

; <<<<<<<<<<<<<<<<<<<<<<<
; <<<<< END OF FILE <<<<<
; <<<<<<<<<<<<<<<<<<<<<<<
PB V6.10 Beta 8 x64
LInux Debian 12

Best regards
StarBootics
The Stone Age did not end due to a shortage of stones !
Fred
Administrator
Administrator
Posts: 18351
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: PB 6.10 B8 - XML Memory leak

Post by Fred »

Please don't post these kind of report, it's useless, because we use the OS automatic memory free to release all the dynamic memory when a program ends. If you want to proves a memory leak, create a loop around the command which is leaking so we can really see the increasing memory usage.
Post Reply