【问题标题】:Why am I getting *** _NSAutoreleaseNoPool(): Object 0x97480b0 of class NSCFDictionary autoreleased with no pool in place - just leaking为什么我得到 *** _NSAutoreleaseNoPool(): NSCFDictionary 类的对象 0x97480b0 自动释放,没有适当的池 - 只是泄漏
【发布时间】:2010-05-12 16:35:39
【问题描述】:

我已经注意到有关此主题的其他几个线程,并尝试将我的线程代码包装为: NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init]; [池释放];

但错误仍然存​​在。

我正在使用静态方法来实例化单词字典。 这是一些代码:

    -(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];
}

关于在新的选择器线程中使用静态类方法,我应该知道些什么?

感谢您的帮助

【问题讨论】:

    标签: objective-c memory-management static autorelease


    【解决方案1】:

    首先,Objective-C 中没有静态方法。有类方法。

    其次,您的代码显示了包装在自动释放池中的两种方法。警告一定来自其他地方。

    最后,您的代码会像筛子一样泄漏。您没有遵循内存管理规则。而且里面还有一些废话。

    具体来说:

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

    除非+setDictionary: 违反内存管理规则,否则上述泄漏。

    这个语句[NSMutableDictionary dictionaryWithContentsOfFile:filePath];实际上什么都不做,除非你对返回值做一些事情。

    另外,mySerializer 正在泄漏。

    尝试在您的代码上运行分析器并解决问题。您还应该阅读thisthis

    【讨论】:

      【解决方案2】:

      啊,[NSMutableDictionary dictionaryWithContentsOfFile:filePath]; 是我试图让字典访问更快的实验的一部分。我应该从这个例子中删除它。

      我刚刚阅读了内存管理规则,明白了 [WordDictionary setDictionary:[[NSMutableDictionary alloc] init]]; 似乎是计划不周的实例化,因为由于引用丢失,我无法从 configDictionary 中释放它。但实际上我不想发布它,它在我的应用程序的整个生命周期中都存在。可能不好的做法是一样的。

      mySerializer一定要在底部放出来。

      我只是想知道类方法是否有关于自动释放池和内存的特殊规则。

      我会查看您发送给我的文件并尝试找出分析器,谢谢您的帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多