NSMutableArray for Object which has NSString property causes memory leak
        Posted  
        
            by user262325
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by user262325
        
        
        
        Published on 2010-04-22T09:42:18Z
        Indexed on 
            2010/04/22
            10:03 UTC
        
        
        Read the original article
        Hit count: 241
        
iphone
|memory-management
Hello everyone
I hope to add objects to a NSMutableArray "myArray", The NSMutableArray is the array for FileObj which has a NSString property "fileName"
#import <UIKit/UIKit.h>
@interface FileObj :  NSObject  {
    NSString *fileName;
}
-(void) setfileName:(NSString *)s ;
-(NSString *) getfileName ;
@end
//
//  File.m//
#import "File.h"
@implementation FileObj
 -(void) setfileName:(NSString *)s ;
{
    fileName=s;
}
-(NSString *) getfileName ;
{
    return fileName;
}
@end
I initialize the myArray here:
NSMutableArray *temarray;
temarray=[[NSMutableArray alloc] init];
self.myArray=temarray;
[temarray release];
the codes to add object to myArray
FileObj *newobj=[[FileObj alloc]init ];
NSString  *fieldValue2 = [[NSString alloc]   initWithUTF8String:@"aaaa"];
[newobj setfileName:fieldValue2];
[myArray addObject:newobj];
[fieldValue2 release]; //**if I enabled the line, it will cause crash**
                       //**if I disable the line, it will cause memory leak**
[newobj release];
Welcome any comment
Thanks
interdev
© Stack Overflow or respective owner