【发布时间】:2011-03-08 11:08:38
【问题描述】:
我已经开始在发布之前清理我的应用程序 - 使用“仪器”泄漏分析器。
我发现了一个无法堵住的泄漏点。所以我建立了一个简单的项目来说明这个问题。请看下面的代码。我在视图上放了一个按钮来测试触发程序“测试”。它总是会产生泄漏。
首先是名为“theObj”的对象的标头和代码
#import <Foundation/Foundation.h>
@interface theObj : NSObject {
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];
}
【问题讨论】:
-
Objective-C?您可能应该标记语言(我会,但我不确定我猜对了)。
-
您在提问时应该使用更多标签,这可以告诉其他人您正在使用的技术并提高获得答案的机会。