raheel
raheel

Reputation: 1417

A memory leak in +initialize

Instruments reports a 100% leak in this method:

+(void)initialize{
    mapper = [[NSMutableDictionary alloc] init];
}

Assuming that Instruments doesn't report false positives, what possible scenarios could lead to this? Multi-threading (although the docs say it's called in a "thread-safe manner")?

This is non-ARC.

Upvotes: 1

Views: 188

Answers (3)

raheel
raheel

Reputation: 1417

The issue was that there were subclasses of this class above. Setting up the allocs in initialize in a dispatch_once block fixed the leaks.

This post from Mike Ash helped in figuring out initialize.

Upvotes: 1

justin
justin

Reputation: 104698

use an autorelease pool:

+(void)initialize {
    @autoreleasepool {
      mapper = [[NSMutableDictionary alloc] init];
    }
}

in this case, creation of the object can result in autoreleased object (e.g. internal or temporary within NSMutableDictionary's implementation). of course, if this were the problem, you would see "…autoreleased with no pool in place -- just leaking" messages in the console.

you should also consider initializing your shared/global stuff lazily, or after your app's done launching.

if you don't see those messages, then this would not apply.

Upvotes: 1

user529758
user529758

Reputation:

The problem (assuming a non-ARC environment) is that you allocate the memory for mapper but you never release it.

Upvotes: 2

Related Questions