如果保留了一个inited对象,那么我拥有它,并将它存储在一个NSArray中,它保留了存储在其中的NSArray,我可以依靠NSArray来查看它已经保留并且不会增加计数,或者我需要运行数组并减少保留计数以确保没有内存泄漏?
我在发布对象时遇到了麻烦.. 为了更好地解释它,我在下面包含了我的代码。
NSTask *task = [NSTask new];
NSTask *grep = [NSTask new];
NSPipe *pipe = [NSPipe new];
[task setStandardError: pipe];
[grep setStandardInput: pipe];
[pipe release];
pipe = [NSPipe new];
[grep setStandardOutput: pipe];
[task launch];
[grep launch];
NSString *string = [[[[[[NSString alloc] initWithData: [[[grep standardOutput] fileHandleForReading] readDataToEndOfFile] encoding: NSASCIIStringEncoding] autorelease] componentsSeparatedByString: @" "] objectAtIndex: 3] substringToIndex: 8];
NSMutableDictionary *dict = [NSMutableDictionary new];
[dict setObject: string forKey: @"myKey"];
[records addObject: dict];
[dict release];
[task release];
[grep release];
[pipe release];
Run Code Online (Sandbox Code Playgroud)
我将如何释放字符串,是否还有其他泄漏?另外,如果我从数组中删除所有records与 …
出于某种原因,当我发布NSArray时,我得到了EXC_BAD_ACCESS异常.这是实施:
-(void) loadAllAlphabets
{
NSBundle *bundle = [NSBundle mainBundle];
NSArray *imagesPath = [[NSArray alloc] init];
imagesPath = [bundle pathsForResourcesOfType:@"png" inDirectory:@"Images"];
alphabets = [[NSMutableArray alloc] init];
NSString *fileName = [[NSString alloc] init];
for(int i=0; i<= imagesPath.count -1 ; i++)
{
fileName = [[imagesPath objectAtIndex:i] lastPathComponent];
CCSprite *sprite = [CCSprite spriteWithFile:fileName];
sprite.userData = [[fileName stringByDeletingPathExtension] uppercaseString];
[alphabets addObject:sprite];
}
// release fileName
[fileName release];
fileName = nil;
[imagesPath release]; // this causes the application to crash with EXC_BAD_ACCESS
// imagesPath = nil; …Run Code Online (Sandbox Code Playgroud) 我正在加载一个像这样的浮点数组:
NSArray *arr= [NSArray arrayWithObjects:
[NSNumber numberWithFloat:1.9],
[NSNumber numberWithFloat:1.7],
[NSNumber numberWithFloat:1.6],
[NSNumber numberWithFloat:1.9],nil];
Run Code Online (Sandbox Code Playgroud)
现在我知道这是正确的做法,但我对零售数量感到困惑.
每个Object都由该[NSNumber numberWithFloat:]方法创建.这给对象保留计数为1 dosnt吗? - 否则该物体将被回收
该arrayWithObjects:方法向每个对象发送保留消息.
这意味着每个对象的保留连续为2.当取消分配数组时,每个对象都被释放,保留计数为1.
我错过了什么?