数组中对象的内存泄漏 [英] Memory leak for object in array

查看:59
本文介绍了数组中对象的内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在发布之前,我已经开始清理我的应用程序-使用仪器"泄漏分析仪.

I've started cleaning up my app before publication - using "Instruments" Leak analyzer.

我发现无法堵漏.因此,我建立了一个简单的项目来说明问题.请参见下面的代码.我在视图上放了一个按钮,以测试测试"过程.它总是会产生泄漏.

I found a leak I can't plug. So I built a simple project to illustrate the problem. Please see code below. I put a button on the view to test fire the procedure "test". It always generates a leak.

首先是名为"theObj"的对象的标头和代码

First the header and code for an object named "theObj"

#import <Foundation/Foundation.h>


@interface theObj : NSObject {

NSString * theWord; } @property(nonatomic,retain)NSString * theWord;

NSString* theWord; } @property (nonatomic,retain) NSString* theWord;

@end

#import "theObj.h"


@implementation theObj
@synthesize theWord;

-(id) initWithObjects: (NSString *) aWord;
{
 if (self = [super init]){
  self.theWord = aWord;
 }
 return self;
}

-(void) dealloc{
[theWord release];
[super dealloc];
}

@end

现在是视图控制器

#import <UIKit/UIKit.h>
#import "theObj.h"

@interface LeakAnObjectViewController : UIViewController {
 NSMutableArray* arrObjects;
}
  - (IBAction)test;
@end

#import "LeakAnObjectViewController.h"

@implementation LeakAnObjectViewController

- (IBAction)test {  
 if (arrObjects == nil)
  arrObjects = [[NSMutableArray alloc] init];

 NSString* aStr = @"first";
 [arrObjects addObject:[[theObj alloc] initWithObjects:aStr]];
 [arrObjects removeAllObjects];
}  

推荐答案

有人真的需要学习

Someone really needs to learn the rules around memory management. Specifically as it pertains to ownership, etc.

这篇关于数组中对象的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆