Question

I'm always really picky about memory leaks and I cannot understand why my garbage collected application leaks. My code is entirely memory-managed and it runs great without garbage collection, not a single leak. However, as soon as I turn on garbage collection it leaks!

Just to prove a point, why does this leak in a garbage collected app? (place this dummy code at applicationDidFinishLaunching:)

NSOpenPanel *panel = [NSOpenPanel openPanel];
[panel beginSheetModalForWindow:window completionHandler:^(NSInteger result) {NSBeep();}];

Also, is there a way to prevent leaks in apps (garbage collected or otherwise) that use Scripting Bridge? it seems as if they all leak, even the sample ones in xcode.

Thanks everybody!

No correct solution

OTHER TIPS

Here's a partial answer.

In most managed languages, each structure capable of holding references to another object embeds information on which offset can hold a memory address. For instance, in each stack frame, there's a place that tells the garbage collector where to look for addresses.

C-based languages like Objective-C don't have that.

For program correctness, it's better to have a little too many objects than to deallocate objects too early. Therefore, the garbage collector in Objective-C looks for patterns that look like addresses, but cannot know for sure if they are indeed addresses. This may (and is almost bound to) result in false positives for references (and is incompatible with certain memory management techniques you could use with C). Therefore, certain objects can outlive their usefulness until some random integer ceases to exist.

Also, how do you tell what's a leak? Did you just compare the memory footprint, or you checked them through the 'Leaks' instrument? Did you try the 'GC Monitor' instrument?

Sorry I forgot to mention it, I use the "Leaks" instrument in Instruments. There's where I see the leaks. I haven't tried the "GC Monitor" instrument yet. The leaks are small and I can't trace them back to my code using the extended detail.

I've always done my memory management the old-fashion way, so in essence I'm a GC newbie.

So this with GC pattern is normal? Should I just ignore it?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top