I am a new Objective-C programmer coming from a C#, VB.NET, Etc. These are all garbage collected languages and for the most part the worst thing you can do is abuse memory usage, because when your program shuts down the memory is reclaimed by the runtime.
However, I am not clear about Objective-C. I get that for the most part its up to us as a developer to manage the allocation, initialization, retention and releasing of objects. I am trying my best to do this and think slowly I am getting the hang of it.
My worry is this: I am not sure if I understand the term memory leak correctly. Does this refer to not properly releasing memory, and then when my application shuts down there is unused dangling memory? IN other words, when my program shuts down the Mac OS doesnt make sure that everything the program was using is cleaned up?
I hope this make sense, its really making sense of the differences after a program shuts down, not so much about memory while the program is running.
I am not sure if I understand the term memory leak correctly. Does this refer to not properly releasing memory, and then when my application shuts down there is unused dangling memory?
No; a memory leak is a leak of memory whilst the application is still running, since OS X reclaims all of the memory when the application terminates. If this was not so, it could cause huge problems because of poor coding and memory management, and would affect the entire system.
It’s basically about retaining an object too much, or not releasing memory enough. For example, when adding objects to an array, this is a common way to go about doing it:
Assuming
someObjectwas allocated correctly, it will start with a retain count of 1. When you add objects to an array, the array callsretainon the object in question, bumping it up to, in this case, 2.When an array is released, it sends
releaseto all of its objects; in this scenario, the retain count would be bumped back down to 1 (assuming no-one else had retained the object). This isn’t 0, sosomeObjectwould still be in existence. IfsomeObjectwere a local pointer, created inside a method, and you no longer had a pointer to that object, then the memory would be hanging there. This is an example of a memory leak, and causes your application to use more memory than it needs, and will suffer until it is terminated.