数组保留问题
我对 Objective-C 相当陌生,大部分内容都很清楚,但是当涉及到内存管理时,我有点不足。目前,我的应用程序所做的是在 NSURLConnection 期间,当我输入一个方法来解析一些数据、将其放入数组并返回时调用方法 -(void)connectionDidFinishLoading:(NSURLConnection *)connection
那个数组。但是我不确定这是否是最好的方法,因为我没有在自定义方法中从内存中释放数组(方法1,请参阅随附的代码)
下面是一个小脚本,可以更好地展示我在做什么
.h文件
#import <UIKit/UIKit.h>
@interface memoryRetainTestViewController : UIViewController {
NSArray *mainArray;
}
@property (nonatomic, retain) NSArray *mainArray;
@end
.m 文件
#import "memoryRetainTestViewController.h"
@implementation memoryRetainTestViewController
@synthesize mainArray;
// this would be the parsing method
-(NSArray*)method1
{
// ???: by not release this, is that bad. Or does it get released with mainArray
NSArray *newArray = [[NSArray alloc] init];
newArray = [NSArray arrayWithObjects:@"apple",@"orange", @"grapes", "peach", nil];
return newArray;
}
// this method is actually
// -(void)connectionDidFinishLoading:(NSURLConnection *)connection
-(void)method2
{
mainArray = [self method1];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
mainArray = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[mainArray release];
[super dealloc];
}
@end
im fairly new to objective-c, most of it is clear however when it comes to memory managment I fall a little short. Currently what my application does is during a NSURLConnection when the method -(void)connectionDidFinishLoading:(NSURLConnection *)connection
is called upon I enter a method to parse some data, put it into an array, and return that array. However I'm not sure if this is the best way to do so since I don't release the array from memory within the custom method (method1, see the attached code)
Below is a small script to better show what im doing
.h file
#import <UIKit/UIKit.h>
@interface memoryRetainTestViewController : UIViewController {
NSArray *mainArray;
}
@property (nonatomic, retain) NSArray *mainArray;
@end
.m file
#import "memoryRetainTestViewController.h"
@implementation memoryRetainTestViewController
@synthesize mainArray;
// this would be the parsing method
-(NSArray*)method1
{
// ???: by not release this, is that bad. Or does it get released with mainArray
NSArray *newArray = [[NSArray alloc] init];
newArray = [NSArray arrayWithObjects:@"apple",@"orange", @"grapes", "peach", nil];
return newArray;
}
// this method is actually
// -(void)connectionDidFinishLoading:(NSURLConnection *)connection
-(void)method2
{
mainArray = [self method1];
}
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
mainArray = nil;
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
- (void)dealloc {
[mainArray release];
[super dealloc];
}
@end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的
-method1
首先创建一个新数组,然后用新数组覆盖它:第一个数组在这里只是泄漏了。您还泄漏了存储在 ivar 中的数组,只需使用合成的 setter 来避免这种情况 - 它会为您保留和释放。
如果您还没有这样做,请阅读 内存可可管理指南。
更好的版本:
Your
-method1
first creates a new array and then overwrites it with a new one:The first array is simply leaked here. You are also leaking the array stored in the ivar, just use the synthesized setter to avoid that - it retains and releases for you.
If you haven't done so yet, read the Memory Management Guide for Cocoa.
A better version:
是的,当
mainArray
被释放时,你的newArray
也会被释放。但这只是在method2
被调用一次的情况下发生的。我们正在讨论引用,因此如果您有
两个变量,则将仅引用
NSArray*
。那么在你的情况下newArray
只是一个本地的,所以没有问题。如果您调用
method2
两次,就会出现此问题:为了避免此问题,您应该确保在用另一个对象覆盖引用之前释放
mainArray
。编辑:没有注意到您创建了两次数组:)不,这不好..
Yes, your
newArray
is released whenmainArray
is released. But this just ifmethod2
is called once.We're talking about references so if you have
both variables will be referencing to just a
NSArray*
. Then in your casenewArray
is just a local so there's no problem.The problem occurs if you call
method2
twice:To avoid this issue you should make sure to release
mainArray
before overwriting the reference with another object.EDIT: didn't noticed that you were creating the array twice :) No, that's not good..