Page 1 of 1

Does this cleanup all memory?

Posted: Tue Oct 06, 2015 4:37 pm
by Kukulkan
Hello,

I'm just wondering if this is really freeing up all memory or if I create a memory-leak here?

Code: Select all

Structure structMail 
  mailaddress.s 
EndStructure 

Structure structUser
  firstName.s 
  lastName.s 
  List mailAddresses.structMail() 
EndStructure 

NewList users.structUser() 

AddElement(users())
users()\firstName = "Klaus" 
users()\lastName = "Meyer" 
AddElement(users()\mailAddresses())
users()\mailAddresses()\mailaddress = "klaus@test.de" 
AddElement(users()\mailAddresses())
users()\mailAddresses()\mailaddress = "klaus.meyer@test.de" 

ClearList(users()); everything cleaned up here?
; FreeList(users()); and is everything cleaned up here?
Is all memory cleaned up here? I'm wondering if PB is also cleaning up all mailAddresses() entries...

Kukulkan

Re: Does this cleanup all memory?

Posted: Tue Oct 06, 2015 5:56 pm
by Keya
my understanding is that Clear clears/resets the list but it remains in memory, and Free releases the list from memory, therefore also effectively clearing it in the process

Re: Does this cleanup all memory?

Posted: Tue Oct 06, 2015 6:59 pm
by PMV
It should, if not it would be a bug ... :?
But yes, it is intended to to free the memory of the mail-strings, too.
As PB-Linked-Lists are internally more then just a (de)allocation
of needed memory you can't assume that a tool like the windows-taskmanager
can show you if that works.

If you need to be sure the mail-adresses are not anymore as wast
in ram, i would overwrite it first.

MFG PMV

Re: Does this cleanup all memory?

Posted: Wed Oct 07, 2015 4:49 am
by Demivec
Just a note on some differences between ClearList() and FreeList().

ClearList() removes all the elements from the list and frees their associated memory, but the list's structure itself still takes up memory. You can immediately add elements to the list again as before (or use any other list functions).

FreeList() performs the same functions as ClearList() and also frees the list's structure and its associated memory. Before adding elements to the list again (or using any other list functions) you would have to use NewList to allocate and initialize the list's structure.

Re: Does this cleanup all memory?

Posted: Wed Oct 07, 2015 7:31 am
by Kukulkan
Hello,

thank you all for the responses. I do not care about if they resist in memory but I don't want to create a memory leak by deletion and filling every now and then. I did a few tests and for me it looks like PB is not only cleaning the main structure but also all "child structures" inside. So, everything seems fine but the documentation does not give any information about cleaning child structures.

Thank you!

Re: Does this cleanup all memory?

Posted: Wed Oct 07, 2015 8:30 am
by c4s
Kukulkan wrote:So, everything seems fine but the documentation does not give any information about cleaning child structures.
Agreed, the documentation is a little too shallow on this but for FreeList() it says: "[...] and release all its associated memory." -- So if you know what's meant by that the information is actually there.


Little off-topic: I think the documentation could be clearer about what exactly needs to be done (or is done automatically like in this example) regarding nested structures with complex datatypes: What is automatically initialized? When do we need InitializeStructure()? What is cleared by FreeList(), ClearList() or ClearStructure() and when to use it? Or what is AllocateStructure() for? All of that in a brief coherent step-by-step guide (is this among others what the empty chapters of the included user guide are for?) would be great.

Re: Does this cleanup all memory?

Posted: Wed Oct 07, 2015 8:53 am
by davido
This may confirm:
http://www.purebasic.fr/english/viewtop ... 08#p371408

However, it would be nice if, as has been suggested, it is stated clearly in the docs.