我有一個類「TestA」並將它的對象放在主函數的NSMutable字典中。第二類名稱爲「ThreadClass」,它具有一個線程。在這個線程中,我從字典中獲取對象並使用它。當我運行該程序時,儀器向我顯示了此線程中的內存泄漏。但我不明白爲什麼內存泄漏,因爲我沒有使用任何新的或分配,複製或保留。我附上了我的程序的完整代碼與主類。需要關於內存管理的qucik幫助。需要關於可可內存管理的幫助
我也有一個問題,如果我有一個有NSString作爲數據成員的類。如果我把這個對象放在NSMutableDictionary中,那麼我如何處理這個內存管理。 thanx
#import <Cocoa/Cocoa.h>
@interface TestA : NSObject {
@public
NSString *strTemp1;
}
@end
#import "TestA.h"
/////////////////////////////////////////////
//Implementation class
@implementation TestA
-(id)init
{
self = [super init];
if (self != nil)
{
//strTemp1 = [[NSString alloc] init];
//printf("\n Temp 1 %d \n", [strTemp1 retainCount]);
}
return self;
}
@end
//Thread Class
#import <Cocoa/Cocoa.h>
#import "TestA.h"
@interface ThreadClass : NSObject {
}
@end
#import "ThreadClass.h"
extern NSMutableDictionary *clData;
@implementation ThreadClass
-(void)Initialize
{
[NSThread detachNewThreadSelector:@selector(AnimateThread:) toTarget:self withObject:nil];
printf("\n<<<<<<<<<<<<<<< Start Animator Thread Called >>>>>>>>>>>>>>>>>>>>>\n");
}
//==========================================================================
- (void) AnimateThread:(id) inObject
{
NSAutoreleasePool *Pool = [[NSAutoreleasePool alloc] init];
NSDate *timeToSleep = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval)5];
while (1)
{
//NSAutoreleasePool *threadPool = [[NSAutoreleasePool alloc] init];
printf("\n Collection Size:%d \n", [clData count]);
TestA *obj1 = [clData objectForKey:@"abc"];
printf("\nThread Memory Counter:%d \n", [obj1 retainCount]);
printf("\nThread Memory Counter:%s \n", [obj1->strTemp1 UTF8String]);
//Sleep Thread for 5 Seconds
[timeToSleep release];
timeToSleep = [NSDate dateWithTimeIntervalSinceNow:(NSTimeInterval)5];
[NSThread sleepUntilDate:(NSDate *)timeToSleep];
//[timeToSleep release];
//[threadPool release];
}
[Pool release];
}
@end
//////////////////////////
Main Class
#import <Cocoa/Cocoa.h>
#import "TestA.h"
#import "ThreadClass.h"
NSMutableDictionary *clData = nil;
int main(int argc, char *argv[])
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
clData = [[NSMutableDictionary alloc] init];
//Creating Thread Class
ThreadClass *obj = [ThreadClass new];
[obj Initialize];
[obj release];
TestA *obj1 = [[TestA alloc] init];
NSString *strKey =[NSString new];
strKey = @"abc";
printf("\n Memory Counter:%d \n", [obj1 retainCount]);
printf("\n Memory Counter:%d \n", [obj1->strTemp1 retainCount]);
NSString *strTemp = @"Test Program";
obj1->strTemp1 = [NSString stringWithFormat:@"%s", [strTemp UTF8String]];
[obj1->strTemp1 retain];
printf("\n Memory Counter:%s \n", [obj1->strTemp1 UTF8String]);
[clData setObject:obj1 forKey:strKey];
printf("\nAfter Memory Counter:%d \n", [obj1 retainCount]);
printf("\nAfter Memory Counter:%d \n", [obj1->strTemp1 retainCount]);
[strKey release];
[obj1 release];
[pool release];
return NSApplicationMain(argc, (const char **) argv);
}
最簡單的方法是使用ARC。見:http://stackoverflow.com/questions/6385212/how-does-the-new-automatic-reference-counting-mechanism-work – tillerstarr
thanx爲您的答覆。但是在這裏我沒有在收藏中創建自己的對象。這是autorelease對象。在主要我用[obj1-> strTemp1 retain];在集合中添加對象之前,我將其引用計數增加爲1.如果我沒有執行此步驟,則程序在線程循環中崩潰。我不明白爲什麼會發生這種情況。 –
您可能需要閱讀內存管理以及屬性。試試這個:https://developer.apple.com/library/mac/#documentation/General/Conceptual/DevPedia-CocoaCore/MemoryManagement.html – NikosM