自动释放的 NSMutableArray 未填充
我想填充这样的数组:
NSMutableArray *array = [self methodThatReturnsAnArray];
在“methodThatReturnsAnArray”方法中,我创建一个这样的数组:
NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];
当我完成填充“arrayInMethod”时,我将返回该数组,为了防止内存泄漏,我正在使用:
return [arrayInMethod autorelease];
但是“数组”变量永远不会被填充。当删除“自动释放”时,它工作正常。我应该怎么做才能确保我释放了返回的对象?
编辑
+ (NSMutableArray *)buildInstants:(NSArray *)huntsArray {
NSMutableArray *goGetObjects = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < [huntsArray count]; i++) {
NSDictionary *huntDict = [huntsArray objectAtIndex:i];
PHGoGet *goGet = [[PHGoGet alloc] init];
goGet.title = [huntDict objectForKey:@"title"];
goGet.description = [huntDict objectForKey:@"description"];
goGet.start = [huntDict objectForKey:@"start"];
goGet.end = [huntDict objectForKey:@"end"];
goGet.ident = [huntDict objectForKey:@"id"];
if ((CFNullRef)[huntDict objectForKey:@"image_url"] != kCFNull) {
goGet.imageURL = [huntDict objectForKey:@"image_url"];
} else {
goGet.imageURL = nil;
}
if ((CFNullRef)[huntDict objectForKey:@"icon_url"] != kCFNull) {
goGet.iconURL = [huntDict objectForKey:@"icon_url"];
} else {
goGet.iconURL = nil;
}
goGet.longitude = [huntDict objectForKey:@"lng"];
goGet.latitude = [huntDict objectForKey:@"lat"];
goGet.companyIdent = [huntDict objectForKey:@"company_id"];
[goGetObjects insertObject:goGet atIndex:i];
[goGet release];
}
return [[goGetObjects copy] autorelease];
}
I want to populate an array like this:
NSMutableArray *array = [self methodThatReturnsAnArray];
In the "methodThatReturnsAnArray"-method I create an array like this:
NSMutableArray *arrayInMethod = [[NSMutableArray alloc] init];
When I'm finished populating "arrayInMethod" I'm returning the array and in order to prevent a memory leak I'm using:
return [arrayInMethod autorelease];
However the "array"-variable is never populated. When removing the "autorelease" it works fine though. What should I do in order to make sure that the returned object i released?
EDIT
+ (NSMutableArray *)buildInstants:(NSArray *)huntsArray {
NSMutableArray *goGetObjects = [[[NSMutableArray alloc] init] autorelease];
for (int i = 0; i < [huntsArray count]; i++) {
NSDictionary *huntDict = [huntsArray objectAtIndex:i];
PHGoGet *goGet = [[PHGoGet alloc] init];
goGet.title = [huntDict objectForKey:@"title"];
goGet.description = [huntDict objectForKey:@"description"];
goGet.start = [huntDict objectForKey:@"start"];
goGet.end = [huntDict objectForKey:@"end"];
goGet.ident = [huntDict objectForKey:@"id"];
if ((CFNullRef)[huntDict objectForKey:@"image_url"] != kCFNull) {
goGet.imageURL = [huntDict objectForKey:@"image_url"];
} else {
goGet.imageURL = nil;
}
if ((CFNullRef)[huntDict objectForKey:@"icon_url"] != kCFNull) {
goGet.iconURL = [huntDict objectForKey:@"icon_url"];
} else {
goGet.iconURL = nil;
}
goGet.longitude = [huntDict objectForKey:@"lng"];
goGet.latitude = [huntDict objectForKey:@"lat"];
goGet.companyIdent = [huntDict objectForKey:@"company_id"];
[goGetObjects insertObject:goGet atIndex:i];
[goGet release];
}
return [[goGetObjects copy] autorelease];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试使用 NSMutableArray 的便捷方法...更改:
To...
array
将返回一个自动释放的对象。Try using the convienence method for
NSMutableArray
... change:To...
array
will return an autoreleased object.首先,我建议您不要从任何方法返回 NSMutableArray。最好使用 NSArray 来避免一些非常难以调试的问题。我的建议是:
声明可变数组并填充它:
然后返回一个自动释放的副本:
最后,当您获取返回的数组时,再次使其可变(仅当您需要更改它时):
完成后使用数组,您可以释放它:
First of all, I recommend you not to return a NSMutableArray from any method. It's better to use NSArray for that to avoid some very difficult to debug problems. My proposition is:
You declare the mutable array and populate it:
Then you return an autoreleased copy:
And finally, when you take the returned array, you make it mutable again (only if you need to change it):
When you're done with the array, you release it: