Ignored retain property in Class method
i've a Class method like this:
+(CCScene *) sceneWithMovie:(NSString*)movieName level:(NSString*)levelName hudLevel:(NSString*)hudName
{
bbsScene* scene = (bbsScene*)[super sceneWithMovie:movieName level:levelName];
ScenePage* hudLayer = (ScenePage*)scene.layer;
UTLOG(@"---> %p RETAIN COUNT: %d",hudLayer.hudLoader,[hudLayer.hudLoader retainCount]);
TouchLevelHelperLoader* loader = [[TouchLevelHelperLoader alloc]initWithContentOfFile:hudName];
hudLayer.hudLoader = loader;
UTLOG(@"---> %p RETAIN COUNT: %d",hudLayer.hudLoader,[hudLayer.hudLoader retainCount]);
[loader release];
UTLOG(@"---> %p RETAIN COUNT: %d",hudLayer.hudLoader,[hudLayer.hudLoader retainCount]);
[hudLayer.hudLoader addSpritesToLayer:hudLayer];
NSInteger sceneNumber = [[[[self class]description] stringByReplacingOccurrencesOfString:@"Scene" withString:@""]intValue];
[hudLayer loadTextPage:sceneNumber fr开发者_运维问答omFile:SCENE_TEXT_FILE];
// return the scene
return scene;
}
The output is:
2011-09-22 10:53:28.477 MP NO VID[598:207] ---> 0x0 RETAIN COUNT: 0
2011-09-22 10:53:28.490 MP NO VID[598:207] ---> 0x64af820 RETAIN COUNT: 2
2011-09-22 10:53:28.491 MP NO VID[598:207] ---> 0x64af820 RETAIN COUNT: 2
When i release loader the data is lost as if i don't call hudLayer.hudLoader = loader; Obviously i set:
@property(nonatomic,retain)TouchLevelHelperLoader* hudLoader;
Any ideas? Maybe the class mothod (+) is the problem?
You shouldn't rely on the retainCount
property.
It is not very reliable because you never know what is done behind the scene.
For example when using Class Clusters like NSString, there are so many things that are done internally in the NSString class that retainCount can't have a real meaning for you. For some cases like NSTimers & so on, objets are also released by the RunLoop (when scheduled on this runloop) but if you don't know it, it is not trivial...
Obviously these two examples (class clusters and runloop retention) are not what you are having here, but what I'm saying here is that the retainCount
property is not something that you should rely on to check if you have a leak.
Moreover, if you have the Garbage Collector activated for your project, release
is a NO-OP (because that's the GC itself that will manage and release the instance)
Actually, the use of retainCount
is prohibited since Xcode4 when you use ARC in your project.
To check if you have leaks in your code, use the Static Analyser ("Build & Analyse" from Xcode Build menu) and/or Instruments "Leaks" tool.
精彩评论