我正在做我在 xCode 4.5 中查找内存泄漏并使用 Leaks 工具的第一步。我发现了几个问题并似乎解决了它们,但这个问题让我望而却步。
这是代码:
RUBEImageInfo* imgInfo = [[[RUBEImageInfo alloc] init] autorelease];
NSString *nm = [NSString stringWithUTF8String:img->name.c_str()];
imgInfo->name = nm;
[imgInfo->name retain]; // I'm using it outside of this method
Leaks 在第二行报告了泄漏,“i”旁边的百分比为 %100。
所以我尝试了两件事:
一,我nm
用这样的autohrleas标记:
NSString *nm = [[NSString stringWithUTF8String:img->name.c_str()] autorelease];
nm
二,我还尝试在分配后调用 release ,imgInfo->name
因此代码如下所示:
imgInfo->name = nm;
[imgInfo->name retain];
[nm release];
但是在这两种情况下,当我运行它并调用[imgInfo->name UTF8String]
.
我错过了什么?
按照 Rob 的回答进行编辑:
这是 RUBEImageInfo 类:
#import "cocos2d.h"
@interface RUBEImageInfo : NSObject {
@public CCSprite* sprite; // the image
@public NSString* name; // the file the image was loaded from
@public class b2Body* body; // the body this image is attached to (can be NULL)
@public float scale; // a scale of 1 means the image is 1 physics unit high
@public float angle; // 'local angle' - relative to the angle of the body
@public CGPoint center; // 'local center' - relative to the position of the body
@public float opacity; // 0 - 1
@public bool flip; // horizontal flip
@public int colorTint[4]; // 0 - 255 RGBA values
}
@end
还有他们:
#import "RUBEImageInfo.h"
@implementation RUBEImageInfo
// Nothing much to see here. Just make sure the body starts as NULL.
-(id)init
{
if( (self=[super init])) {
body = NULL;
}
return self;
}
-(void) dealloc {
[name release];
[super dealloc];
}
@end