为什么我得到 *** _NSAutoreleaseNoPool(): Object 0x97480b0 of class NSCFDictionary autoreleased with no pool in place - 只是泄漏

发布于 2024-09-01 11:30:00 字数 2013 浏览 1 评论 0原文

我已经注意到有关此主题的其他几个线程,并尝试使用以下方式包装我的线程代码: 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];
}

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

感谢您的帮助

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

横笛休吹塞上声 2024-09-08 11:30:00

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

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

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

具体来说:

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

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

除非您对返回值执行某些操作,否则此语句 [NSMutableDictionary DictionaryWithContentsOfFile:filePath]; 实际上不会执行任何操作。

此外,mySerializer 正在泄漏。

尝试对您的代码运行分析器并修复问题。您还应该阅读这个

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.

默嘫て 2024-09-08 11:30:00

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

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

mySerializer 绝对应该在底部释放。

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

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

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 instantiation 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文