【问题标题】:Getting leaks from substringWithRange从 substringWithRange 获取泄漏
【发布时间】:2011-10-22 09:18:32
【问题描述】:

我在使用 substringWithRange 时遇到了泄漏,如下面的代码行所示。我虽然所有这些功能都是自动释放,你不需要手动分配/释放它们。

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

我很惊讶我有一些泄漏,尽管我正在安静地管理内存。

感谢您的建议和提示。 -代码

【问题讨论】:

  • 你能提供一些关于哪些类型的对象被泄露的信息吗?
  • NSCFString 是被泄露的对象。

标签: iphone objective-c ios


【解决方案1】:

在您的 dealloc 中,只需释放所有字符串 ivars:

[_teamName release];
etc...

或者,您可以这样做:

[self.teamName release];
etc...

在这种情况下,我更喜欢直接使用 ivars。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-10-05
    • 2011-04-26
    • 2015-05-06
    • 1970-01-01
    • 2015-07-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多