views:

167

answers:

2

I have noted several other threads on this topic and have tried wrapping my threaded code with: NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; [pool release];

but the errors still come.

I am using a static method to instantiate a dictionary of words. Here is some code:

    -(id)init
     [NSThread detachNewThreadSelector:@selector(loadDictionary:) toTarget:[IntroScreen class] withObject:nil];
     [NSThread setThreadPriority:1.0];
     return self;
    }

    +(void)loadDictionary:(id)param
    {
     NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
     [[SimpleAudioEngine sharedEngine] preloadEffect:@"click.wav"];
     [[SimpleAudioEngine sharedEngine] preloadEffect:@"pop.wav"];
     [[SimpleAudioEngine sharedEngine] preloadEffect:@"dink.wav"];
     [[SimpleAudioEngine sharedEngine] playBackgroundMusic:@"musicloop.wav"];
     [WordDictionary configDictionary];
     [pool release];
    }

+(void)configDictionary
{
 NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
 Serializer * mySerializer = [[Serializer alloc] init];

 [WordDictionary setDictionary:[mySerializer readApplicationPlist:@"x"]];
 NSString * string;
 NSString *filePath = [[[NSBundle mainBundle] resourcePath] 
        stringByAppendingPathComponent:@"x.txt"];
 NSString *info = [NSString stringWithContentsOfFile:filePath encoding:NSUTF8StringEncoding error:nil];
 NSArray *arrayOfLines = [info componentsSeparatedByString:@"\r\n"];
 [WordDictionary setDictionary:[[NSMutableDictionary alloc] init]];
 [NSMutableDictionary dictionaryWithContentsOfFile:filePath];
 int len = [arrayOfLines count];
 for(int i = 0; i < len; i++)
 {
  string = [arrayOfLines objectAtIndex:i];
  NSString * blankString = [NSString stringWithString:@""];
  [[WordDictionary dictionary] setObject:blankString forKey:string];
  double calc = ((double)i / (double)len) * 100.0;
  [WordDictionary setProgress:(int)calc];
 }

 [mySerializer writeApplicationPlist:[WordDictionary dictionary] toFile:@"s"]; 
 [WordDictionary setProgress:100];
 [pool release];
}

Is there something I should know about using static class methods with new selector threads?

Thank you for your help

+1  A: 

First, there are no static methods in Objective-C. There are class methods.

Secondly, your code shows both methods wrapped in autorelease pools. The warning must be coming from somewhere else.

Finally, your code leaks like a sieve. You aren't following the memory management rules. And there are some nonsense statements in there.

Specifically:

[WordDictionary setDictionary:[[NSMutableDictionary alloc] init]];

Unless +setDictionary: is breaking the memory management rules, the above leaks.

This statement [NSMutableDictionary dictionaryWithContentsOfFile:filePath]; effectively does nothing unless you do something with the return value.

Also, mySerializer is leaking.

Try running the analyzer over your code and fixing the problem. You should also read this and this.

bbum
A: 

Ah the [NSMutableDictionary dictionaryWithContentsOfFile:filePath]; was part of an experiment I was attempting to make the dictionary access faster. I should have removed it from this example.

I have just read the memory management rules, and understand that [WordDictionary setDictionary:[[NSMutableDictionary alloc] init]]; appears to be poorly planned instatiation because I have no way to release it from within configDictionary as the reference is lost. But actually I don't ever want to release it, it lives for the entire lifetime of my application. Probably bad practice just the same.

mySerializer should definitely be released at the bottom.

I was just wondering if class methods had any special rules regarding autorelease pools and memory.

I will look over the documents you sent me and try to figure out the Analyzer, thank you for your help.

Benjamin Taller