我在使用substringWithRange时遇到了泄漏,如下面的代码行所示。我虽然所有这些功能都是Autorelease,但您不需要手动分配/释放它们。
NSCFString是泄露的对象。
我做错了什么?
aLTR.drew = [substring substringWithRange:NSMakeRange(match.location+1, (match2.location-(match.location+1)))];
我要做的是提取子字符串并将其存储到我的存储类中。以下代码。
#import <Foundation/Foundation.h>
@interface LeagueTableRow : NSObject
{
NSString *_teamName;
NSString *_played;
NSString *_won;
NSString *_drew;
NSString *_lost;
NSString *_goalsFor;
NSString *_goalsAgainst;
NSString *_points;
}
@property(nonatomic, copy) NSString *teamName;
@property(nonatomic, copy) NSString *played;
@property(nonatomic, copy) NSString *won;
@property(nonatomic, copy) NSString *drew;
@property(nonatomic, copy) NSString *lost;
@property(nonatomic, copy) NSString *goalsFor;
@property(nonatomic, copy) NSString *goalsAgainst;
@property(nonatomic, copy) NSString *points;
-(id)init;
@end
#import "LeagueTableRow.h"
@implementation LeagueTableRow
@synthesize teamName = _teamName;
@synthesize played = _played;
@synthesize won = _won;
@synthesize drew = _drew;
@synthesize lost = _lost;
@synthesize goalsFor = _goalsFor;
@synthesize goalsAgainst = _goalsAgainst;
@synthesize points = _points;
-(id)init
{
self = [super init];
return self;
}
-(void) dealloc
{
self.teamName = nil;
self.played = nil;
self.won = nil;
self.drew = nil;
self.lost = nil;
self.goalsFor = nil;
self.goalsAgainst = nil;
self.points = nil;
[super dealloc];
}
@end
我很安静,但是我得到了一些泄漏,虽然我正整洁地管理着记忆。
感谢您的建议和提示。 -code
答案 0 :(得分:3)
在你的dealloc中,只需释放所有字符串ivars:
[_teamName release];
etc...
或者,您可以这样做:
[self.teamName release];
etc...
在这种情况下,我更喜欢直接使用ivars。