views:

318

answers:

3

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.

A: 

No, the OS should free all memory when you app quits. Memory leaks will crash YOUR app if not handled. To keep it simple say the devices has 10mb of ram and you keep leaking memory every time you call a function. Sooner or later you're going to run out of ram and your app will crash.

Apple provides a great tool "Instruments" that allows you to track and fix memory leaks. I suggest you run you app through this every now and then just to check for leaks. I've gotten to the point that I always find the place I'm going to release the object when I call alloc. If I call alloc and it's not autorelease then I have to find the place to put the release.

Ryan Detzel
+1  A: 

The OS does release the memory, but you can leak up until then. So if you are getting a report of leaks at shutdown, that's just the objects that weren't released yet (but the memory is now reclaimed).

I wrote the blog entry on understanding objective-c memory management for the iPhone.

http://loufranco.com/blog/files/managing-memory-iphone.html

and this one on the CLang static analysis tool which I have found invaluable for finding leaks by examining the source

http://loufranco.com/blog/files/scan-build-iphone.html

Lou Franco
+1  A: 

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:

NSMutableArray *array; //Pointer to some already allocated array

[array addObject:someObject];
[someObject release];

Assuming someObject was allocated correctly, it will start with a retain count of 1. When you add objects to an array, the array calls retain on the object in question, bumping it up to, in this case, 2.

When an array is released, it sends release to 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, so someObject would still be in existence. If someObject were 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.

Perspx