views:

344

answers:

1

How does the XCode Instrument Leak tool figure out if an object is a leak or just something not released yet? I'm pretty new to Objective C, the leak tool detects a leak in the code I work with. But the code looks sound to me. So just wondering how much can I trust this tool?

+3  A: 

A "leak" as an object that's still allocated, but your application no longer has a reference pointing to that object. Since you no longer have a reference, there's no way you will be able to release the object, thus it's a leak.

As the leaks(1) man page says:

leaks identifies leaked memory -- memory that the application has allocated, but has been lost and cannot be freed. Specifically, leaks examines a specified process's memory for values that may be pointers to malloc-allocated buffers. Any buffer reachable from a pointer in writable memory, a register, or on the stack is assumed to be memory in use. Any buffer reachable from a pointer in a reachable malloc-allocated buffer is also assumed to be in use. The buffers which are not reachable are leaks; the buffers could never be freed because no pointer exists in memory to the buffer, and thus free() could never be called for these buffers

You might also want to look into the ObjectAlloc tool in Instruments.

David Gelhar
Thanks, I understand what leak is, just wondering if XCode has a reputation of giving false alarms. Thanks for pointing out ObjectAlloc tool, I found what the problem is with its help. There is indeed a leak.
Jiayao Yu
The leaks tool has gotten much much better over time. It rarely spits out a false positive these days. Very rarely.
bbum