Cocoa内存管理NSArray与对象 [英] Cocoa Memory Management NSArray with Objects

查看:137
本文介绍了Cocoa内存管理NSArray与对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有麻烦释放对象..为了更好地解释它我已经包括我的代码下面。

  NSTask *任务= [NSTask new]; 
NSTask * grep = [NSTask new];
NSPipe * pipe = [NSPipe new];

[task setStandardError:pipe];
[grep setStandardInput:pipe];
[pipe release];
pipe = [NSPipe new];
[grep setStandardOutput:pipe];

[任务启动];
[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];

[任务发布];
[grep release];
[pipe release];

我如何释放字符串,还有其他泄漏?此外,如果我从记录删除所有的 removeAllObjects ,是一切释放好了吗?

只有一个泄漏点,该数组不能被释放,并且始终可用。



感谢任何帮助!

解决方案

Objective-C中的内存管理有一个基本规则


如果您使用名称以alloc或new开头或包含copy(例如alloc,newObject或mutableCopy)的方法创建对象,或者如果您向其发送了保留消息,则获取对象的所有权。您有责任放弃拥有使用release或autorelease的对象的所有权。


因此每次调用 new

code>或者 autorelease ,您的代码示例中的code>应该与 release NSArray 以及代码中的大多数其他对象不是使用任何一个创建的,因此不需要发布。 [NSString alloc] 是自动释放的,所以它得到照顾。集合管理自己的项目,根据需要保留和释放它们:当插入项目时,它被保留;当它被删除,它的释放。



如果您有一个不平衡的您创建的第一个 NSPipe 。在为grep的标准输出创建管道之前释放它。也许你只是把它从样本,但你也没有设置任何参数的grep任务。


I'm having troubles releasing objects.. To explain it better I have included my code below.

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

How would I release the string and are there any other leaks? Also, if I remove everything from the array records with removeAllObjects, is everything released okay then too? The array should never be released and be available at all time, I'm just worrying about its objects.

Edit: The only leak pointed out had to do with the NSPipe and should be fixed in the code.

Thanks for any help!

解决方案

Memory management in Objective-C has one fundamental rule:

You take ownership of an object if you create it using a method whose name begins with "alloc" or "new" or contains "copy" (for example, alloc, newObject, or mutableCopy), or if you send it a retain message. You are responsible for relinquishing ownership of objects you own using release or autorelease. Any other time you receive an object, you must not release it.

Thus every call to new in your code sample should be balanced with a call to release or autorelease. The NSArray, along with most other objects in the code, isn't created with either, so it doesn't need to be released. The [NSString alloc] is autoreleased, so it's taken care of. Collections manage their own items, retaining and releasing them as necessary: when an item is inserted, it's retained; when it's removed, it's released. Dictionary keys are copied rather than retained.

Where you've got an unbalanced new (and hence leak) is the first NSPipe you created. Release it before creating the pipe for grep's standard output. Perhaps you simply left it out of the sample, but you're also not setting any arguments for the grep task.

这篇关于Cocoa内存管理NSArray与对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆