我有一个nsmutablearray,我需要通过应用程序,所以我在应用程序委托中声明它并在应用程序委托的dealloc方法中释放它。这是代码。
@interface AppDelegate : UIResponder <UIApplicationDelegate>{
NSMutableArray *arr1;
IBOutlet UINavigationController *navConroller;
}
@property (strong, nonatomic) IBOutlet UIWindow *window;
@property (nonatomic, retain) UINavigationController *navConroller;
@property (nonatomic, retain) NSMutableArray *arr1;
@implementation AppDelegate
@synthesize navConroller;
@synthesize window = _window;
@synthesize arr1;
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.arr1 = [[NSMutableArray alloc] init];
[self.window addSubview:navConroller.view];
[self.window makeKeyAndVisible];
return YES;
}
- (void)dealloc
{
[self.arr1 release];
[_window release];
[super dealloc];
}
当我检查内存性能工具时,它会显示内存泄漏
self.arr1 = [[NSMutableArray alloc] init];
我在不同的类中使用此数组。任何建议将不胜感激。
我在那个地方使用了Pieter Gunst的回答和泄漏停止。但它正在另一个地方展示。在哪里我削减json并将记录存储在arr1中。这是代码。
-(void) apiCall:(NSString *)para1 {
SBJSON *parser = [[SBJSON alloc] init];
para1 = [para1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
para1 = [para1 stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *url = [[[NSString alloc] initWithFormat:@"my api url",para1] autorelease];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *json_string = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];
arr1 = [parser objectWithString:json_string error:nil];
[json_string release];
[parser release];
}
现在泄漏显示在下一行。
arr1 = [parser objectWithString:json_string error:nil];
有什么建议吗?
答案 0 :(得分:2)
您已分配数组并通过访问属性保留它。 更改以下代码行;
self.arr1 = [[NSMutableArray alloc] init];
以下任何一行......
arr1 = [[NSMutableArray alloc] init];
OR
self.arr1 = [NSMutableArray array];
OR
NSMutableArray *tArr1 = [[NSMutableArray alloc] init];
self.arr1 = tArr1;
[tArr1 release];
答案 1 :(得分:1)
尝试@synthesize arr1 = _arr1;
和_arr1 = [[NSMutableArray alloc] init];
答案 2 :(得分:0)
正如Apurv已经提到的那样使用
self.arr1 = [NSMutableArray array];
或
self.arr1 = [[[NSMutableArray alloc] init] autorelease];
同时制作相同类型的所有属性(不要强力混合并保留):
@property (strong, nonatomic)
对于dealloc方法,使用自动生成的getter / setter:
- (void)dealloc {
self.arr1 = nil;
self.window = nil;
[super dealloc];
}
提示:
由于
,主题启动器不使用ARC[super dealloc];
导致错误“ARC禁止发送'dealloc'的显式消息”