gpt4 book ai didi

iphone - 从 substringWithRange 获取泄漏

转载 作者:行者123 更新时间:2023-11-28 20:44:28 26 4
gpt4 key购买 nike

我在使用 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

虽然我正在安静、整洁地管理内存,但我还是感到有些泄漏。

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

最佳答案

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

[_teamName release];
etc...

或者,您可以:

[self.teamName release];
etc...

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

关于iphone - 从 substringWithRange 获取泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6982491/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com