希望將對象添加到一個的NSMutableArray「myArray的」所述的的NSMutableArray是陣列FileObj文件,其具有的NSString屬性「文件名」的NSMutableArray對於具有的NSString屬性對象導致內存泄漏
#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
我初始化MYARRAY這裏:
NSMutableArray *temarray;
temarray=[[NSMutableArray alloc] init];
self.myArray=temarray;
[temarray release];
代碼添加對象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];
歡迎任何評論
感謝
InterDev中
請再試, 的NSString * fieldValue2 = [[[NSString的頁頭] initWithUTF8String:rowData]自動釋放]; – Tirth 2010-04-22 09:53:10