array retain question

Posted by Cosizzle on Stack Overflow See other posts from Stack Overflow or by Cosizzle
Published on 2010-06-08T00:07:17Z Indexed on 2010/06/08 0:12 UTC
Read the original article Hit count: 575

Filed under:
|
|

Hello, 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

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about nsarray