数组保留问题

发布于 2024-09-04 10:40:43 字数 1653 浏览 1 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

爱殇璃 2024-09-11 10:40:43

您的 -method1 首先创建一个新数组,然后用新数组覆盖它:

NSArray *newArray = [[NSArray alloc] init]; // first array, retained
newArray = [NSArray arrayWithObjects:...];  // second array, auto-released, 
                                            // pointer to first one lost

第一个数组在这里只是泄漏了。您还泄漏了存储在 ivar 中的数组,只需使用合成的 setter 来避免这种情况 - 它会为您保留和释放。

如果您还没有这样做,请阅读 内存可可管理指南

更好的版本:

- (NSArray *)method1 {
    NSArray *newArray = [NSArray arrayWithObjects:...];    
    return newArray;
}

- (void)method2 {
    self.mainArray = [self method1];
}

Your -method1 first creates a new array and then overwrites it with a new one:

NSArray *newArray = [[NSArray alloc] init]; // first array, retained
newArray = [NSArray arrayWithObjects:...];  // second array, auto-released, 
                                            // pointer to first one lost

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:

- (NSArray *)method1 {
    NSArray *newArray = [NSArray arrayWithObjects:...];    
    return newArray;
}

- (void)method2 {
    self.mainArray = [self method1];
}
染年凉城似染瑾 2024-09-11 10:40:43

是的,当 mainArray 被释放时,你的 newArray 也会被释放。但这只是在 method2 被调用一次的情况下发生的。

我们正在讨论引用,因此如果您有

newArray = something
mainArray = newArray
[mainArray release]

两个变量,则将仅引用 NSArray*。那么在你的情况下 newArray 只是一个本地的,所以没有问题。

如果您调用 method2 两次,就会出现此问题:

newArray = something
mainArray = newArray
newArray = something2
mainArray = newArray <- old reference is lost
[mainArray release] <- just something2 is released

为了避免此问题,您应该确保在用另一个对象覆盖引用之前释放 mainArray

编辑:没有注意到您创建了两次数组:)不,这不好..

Yes, your newArray is released when mainArray is released. But this just if method2 is called once.

We're talking about references so if you have

newArray = something
mainArray = newArray
[mainArray release]

both variables will be referencing to just a NSArray*. Then in your case newArray is just a local so there's no problem.

The problem occurs if you call method2 twice:

newArray = something
mainArray = newArray
newArray = something2
mainArray = newArray <- old reference is lost
[mainArray release] <- just something2 is released

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..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文