【发布时间】:2012-02-20 09:06:16
【问题描述】:
我创建的类中的仪器出现内存泄漏。这是课程:
.h
#import <Foundation/Foundation.h>
@interface RDItem : NSObject {
}
@property int Id;
@property (nonatomic, retain) NSString *nombre;
@property (nonatomic, retain) NSString *thumbnail;
@property (nonatomic, retain) NSString *thumbnailPush;
@property int defaultColorId;
@property int idTema;
@property (nonatomic, retain) NSString *selectedFrame;
@property (nonatomic, retain) NSString *mergedFrame;
@property (nonatomic, retain) NSMutableArray *colors;
@property (nonatomic, retain) NSMutableArray *textures;
@property (nonatomic, retain) NSMutableArray *styles;
-(void)initialize;
@end
.m
#import "RDItem.h"
@implementation RDItem
@synthesize Id;
@synthesize nombre;
@synthesize thumbnail;
@synthesize thumbnailPush;
@synthesize defaultColorId;
@synthesize idTema;
@synthesize selectedFrame;
@synthesize mergedFrame;
@synthesize colors;
@synthesize textures;
@synthesize styles;
-(void)initialize
{
colors = [[NSMutableArray alloc] init];
textures = [[NSMutableArray alloc] init];
styles = [[NSMutableArray alloc] init];
}
-(void)dealloc
{
[colors release];
[textures release];
[styles release];
}
@end
这个类有 3 个 NSMutableArray,我将在其中存储数据。为了准备和初始化这个类,我开发了方法 initialize 来创建 3 个数组。在 dealloc 中被释放。
每次使用此类时,泄漏工具都会检测到泄漏,因为初始化方法。
初始化这些数组的最佳方法是什么?
谢谢。
编辑
您好,我已经用 RDItem 解决了泄漏问题,但现在又出现了一个非常相似的类:
.h
#import <Foundation/Foundation.h>
@interface RDTema : NSObject {
}
@property int Id;
@property (nonatomic, retain) NSString *idManifest;
@property (nonatomic, retain) NSString *idTema;
@property (nonatomic, retain) NSString *nombre;
@property (nonatomic, retain) NSString *thumbnail;
@property (nonatomic, retain) NSString *thumbnailPush;
@property (nonatomic, retain) NSMutableArray *items;
@property (nonatomic, retain) NSMutableArray *colors;
@property (nonatomic, retain) NSMutableArray *textures;
@property (nonatomic, retain) NSMutableArray *styles;
-(void)initialize;
@end
.m
#import "RDTema.h"
@implementation RDTema
@synthesize Id;
@synthesize idManifest;
@synthesize idTema;
@synthesize nombre;
@synthesize thumbnail;
@synthesize thumbnailPush;
@synthesize items;
@synthesize colors;
@synthesize textures;
@synthesize styles;
-(void)initialize
{
/*
self.items = [[NSMutableArray alloc] init];
self.colors = [[NSMutableArray alloc] init];
self.textures = [[NSMutableArray alloc] init];
self.styles = [[NSMutableArray alloc] init];
*/
self.items = [NSMutableArray array];
self.colors = [NSMutableArray array];
self.textures = [NSMutableArray array];
self.styles = [NSMutableArray array];
}
-(void)dealloc
{
[idManifest release];
[idTema release];
[nombre release];
[thumbnail release];
[thumbnailPush release];
[items release];
[colors release];
[textures release];
[styles release];
[super dealloc];
}
现在我在这些行中发现了一个漏洞:
self.items = [NSMutableArray array];
self.colors = [NSMutableArray array];
self.textures = [NSMutableArray array];
self.styles = [NSMutableArray array];
任何人都可以解释为什么现在发生在这个类而不是 RDItem 类中?都是一样的:(
谢谢。
【问题讨论】:
-
虽然实现似乎并没有使用您共享的代码行造成真正的内存泄漏。虽然我分享了一个更好的实现。
-
您的代码没有泄漏。但我有几个问题:1)你调用了多少次“初始化”方法? 2)你释放插入可变数组的对象吗?
标签: iphone xcode instruments memory-leaks