RE: [fltk.general] Examples create a ton of dynamic visual objects via new but are never deleted!!! [General Use]

15 views
Skip to first unread message

MacArthur, Ian (Leonardo, UK)

unread,
Sep 30, 2016, 5:00:35 AM9/30/16
to fltkg...@googlegroups.com

> Understand that when a process exits, all memory is automatically
> freed.
>
> There's no such thing as a program leaking memory when it exits;
> all memory it allocated with new/malloc are freed by the OS.


Indeed; there are classes of thing that may persist beyond process termination, and the program does need to make explicit efforts to release these (such as named pipes used for IPC, some types of file handle, and so forth.) The actual types of things tend to vary from OS to OS, but in general they are more "specialist" anyway.

But in the general run of things, when the OS reaps an expired process it will return all the allocated memory, close off file pointers that have been left dangling and so forth, as part of the normal process clean-up.

So, whilst it is *possible* to allocate resources that persist beyond process termination, it usually takes some effort, and I'd imagine that a programmer who had done so would be "expected" to tidy up after themselves!
Normal memory allocations (new | malloc) will be reaped by the process clean-up anyway, and usually much more efficiently than you can do "manually". So they will not leak.

Leaks usually occur during runtime, when a process repeatedly allocates a block (for example in a loop) but fails to return it before the next iteration. (What it should do is return the allocation first, or (often the better choice) simply re-use the prior allocation on subsequent iterations.)


Leonardo MW Ltd
Registered Office: Sigma House, Christopher Martin Road, Basildon, Essex SS14 3EL
A company registered in England & Wales. Company no. 02426132
********************************************************************
This email and any attachments are confidential to the intended
recipient and may also be privileged. If you are not the intended
recipient please delete it from your system and notify the sender.
You should not copy it or use it for any purpose nor disclose or
distribute its contents to any other person.
********************************************************************
Reply all
Reply to author
Forward
0 new messages