Objective-Cのリークについて

フォーラム(掲示板)ルール
フォーラム(掲示板)ルールはこちら  ※コードを貼り付ける場合は [code][/code] で囲って下さい。詳しくはこちら
obott

Objective-Cのリークについて

#1

投稿記事 by obott » 14年前

iPad/iPhone用のプログラムです。
Xcode4.02を使用してiOS4.3の環境です。

以下のプログラム(再現する最小に近い短さにした)を

ヘッダーファイル Test_Memory00.h

コード:

#import <Foundation/Foundation.h>

@interface		Master0 : NSObject<NSCopying>	{

	int					m_No;
	NSString*			m_Name;

}

+ (id)test0;
- (id)copyWithZone:(NSZone*)zone;
- (id)init;

@property (nonatomic)int					m_No;
@property (nonatomic, retain)NSString*		m_Name;

@end


@interface		GetTester : NSObject		{

	NSDateFormatter*		m_DateFormatter;
	NSDateFormatter*		m_DateFormatterNTZ;

}

-(id)init;
-(NSString*)getHttp:(NSString*)uStr;
-(NSArray*)divideLineBrake:(NSString*)sStr;
-(NSArray*)GetMaster;

@end


@interface		Test_Memory00 : NSObject		{

	NSString*			m_iti1Str;
	NSString*			m_iti2Str;
	NSString*			m_iti3Str;
	GetTester*			p_GetTester;
	NSArray*			m_TstAry;

}


-(id)init;
-(void)dealloc;
-(void)testFunc;

@end

ソースファイル Test_Memory00.m

コード:


#import		"Test_Memory00.h"


@implementation		Master0

@synthesize			m_No;
@synthesize			m_Name;

+(id)test0		{

	self = [[[self alloc] init] autorelease];
	return		self;
}


-(id)init		{

	[super init];
	m_No = 0;
	m_Name = Nil;
	return		self;
}


- (id)copyWithZone:(NSZone*)zone		{

	Master0*		clone;

	clone = [[[self class] allocWithZone:zone] init];
//	clone = [[[[self class] allocWithZone:zone] init] autorelease];
	clone.m_No = self.m_No;
	clone.m_Name = [NSString stringWithString:self.m_Name];
//	clone.m_Name = [NSString stringWithFormat:@"%@", self.m_Name];
	return		clone;
}

@end


@implementation		GetTester

-(id)init		{
	[super init];
	return		self;
}


- (void)dealloc		{
	[super dealloc];
}


-(NSString*)getHttp:(NSString*)uStr		{
	NSString*				retStr;

	retStr = Nil;
	retStr = @"0,ゴミ,慶應\r\n1,ゴミ,明治\r\n2,ゴミ,大正\r\n3,ゴミ,昭和\r\n4,ゴミ,平成\r\n5,ゴミ,未来";
	return			retStr;
}


-(NSArray*)divideLineBrake:(NSString*)sStr		{
	NSArray*			retAry = nil;
	NSString*			tRsltStr;
	NSString*			tStr;
	NSRange				tRng0;
	NSRange				tRng1;
	NSMutableArray*			tMary;
	int					Len;


	tRsltStr = [NSString stringWithString:sStr];
	Len = tRsltStr.length;
	tMary = [NSMutableArray arrayWithCapacity:100];
	tRng0 = NSMakeRange(0, Len);
	while ( tRng0.length > 0 ) {
		tRng1 = [tRsltStr lineRangeForRange:NSMakeRange(tRng0.location, 0)];
		tStr = [tRsltStr substringWithRange:tRng1];
		tStr = [tStr stringByReplacingOccurrencesOfString:@"\r\n" withString:@""];
		[tMary addObject:tStr];
		tRng0.location = NSMaxRange(tRng1);
		tRng0.length -= tRng1.length;
	}
	retAry = [tMary copy];
	[retAry autorelease];
	return		retAry;
}


-(NSArray*)GetMaster		{

	NSArray*			retAry = Nil;
	int					Cnt;
	int					i;
	NSCharacterSet*		cComma;
	NSString*			rStr;
	NSString*			lStr;
	NSArray*			tLineAry = nil;
	Master0*			tStCls;

	retAry = Nil;
	rStr = [self getHttp:@"/getMaster"];
	tLineAry = [self divideLineBrake:rStr];
	Cnt = [tLineAry count];
	retAry = [[NSArray alloc] init];
	for ( i = 0; i < Cnt; i++ ) {
		NSAutoreleasePool*	LoopPl = [[NSAutoreleasePool alloc] init];
		NSArray*			dAry = nil;
		lStr = [tLineAry objectAtIndex:i];
		cComma = [NSCharacterSet characterSetWithCharactersInString:@","];
		dAry = [lStr componentsSeparatedByCharactersInSet:cComma];
//		[dAry autorelease];
		if ( [dAry count] == 3 ) {
			tStCls = [Master0 test0];
			tStCls.m_No	= [[dAry objectAtIndex:0] intValue];
			tStCls.m_Name	= [dAry objectAtIndex:2];
			[retAry autorelease];
			retAry  = [retAry arrayByAddingObject:tStCls];
			[retAry retain];
		}
		[LoopPl release];	
	}
	[retAry autorelease];
	return		retAry;
}

@end


@implementation		Test_Memory00

-(id)init		{
	[super init];
	p_GetTester = [[GetTester alloc] init];
	m_TstAry = Nil;
	return		self;
}


-(void)dealloc		{
	[p_GetTester release];
	if ( m_TstAry ) {
		[m_TstAry release];
		m_TstAry = Nil;
	}
	[super dealloc];
}


-(void)testFunc		{
	m_TstAry = [p_GetTester GetMaster];
	[m_TstAry retain];
}

@end


アプリケーションデリゲートのdidFinishLaunchingWithOptionsの中で

コード:

	Test_Memory00*			pTest = [[Test_Memory00 alloc  ]init];
	[pTest testFunc];
	[pTest release];
の様にコールしていますがインスツルメントのリークでcomponentsSeparatedByCharactersInSetの所でリークしていると言われます。
これはどのようにすると直りますか?
ご存知の方よろしくお願いします。

Enna
記事: 8
登録日時: 14年前
連絡を取る:

Re: Objective-Cのリークについて

#2

投稿記事 by Enna » 14年前

ちょっと詳しくないので、詳しい方がきてくださればそちらに従ってください。

retStr = @"0,ゴミ,慶應\r\n1,ゴミ,明治\r\n2,ゴミ,大正\r\n3,ゴミ,昭和\r\n4,ゴミ,平成\r\n5,ゴミ,未来"
この最後に\r\nを入れてもリークしますか?
componentsSeparatedByCharactersInSetの時、一番最後のループ時にretainCountが4になっているように思います。

obott

Re: Objective-Cのリークについて

#3

投稿記事 by obott » 14年前

ヘッダーとソースをDLして実際にシミュレータまたは実機で動かして頂けると幸いです。

その為にこの掲示板(結構な大きさのソースも貼れる)に書込みました。

困っています、、引き続き対策方法を御存知の方、、
よろしくお願いします。

アバター
h2so5
副管理人
記事: 2212
登録日時: 14年前
住所: 東京
連絡を取る:

Re: Objective-Cのリークについて

#4

投稿記事 by h2so5 » 14年前

Master0クラスのメンバ m_Nameがリークしてる気がします。

obott

Re: Objective-Cのリークについて

#5

投稿記事 by obott » 14年前

h2so5さん ありがとうございます

>Master0クラスのメンバ m_Nameがリークしてる気がします。
ご指摘を参考に
Master0クラスに以下の関数を追加するとリークが文句を言わなくなりました。

- (void)dealloc {
if ( m_Name ) {
[m_Name release];
m_Name = Nil;
}
[super dealloc];
}

閉鎖

“C言語何でも質問掲示板” へ戻る