I am working on multi-threading using the following codes in XCode4:
#import <Foundation/Foundation.h>
bool trigger = false;
NSLock *theLock=[NSLock new];
@interface Metronome : NSObject
+(void)tick:(id)param;
@end
@implementation Metronome
+(void)tick:(id)param{
while(1)
{
NSLog(@"TICK\n");
usleep(1000000);
[theLock lock];
trigger = true;
[theLock unlock];
}
}
@end
int main()
{
[NSThread detachNewThreadSelector:@selector(tick:)
toTarget:[Metronome class] withObject:nil];
}
There is no compiling error, but during execution the console pops up the following warning:
objc[688]: Object 0x100300ff0 of class NSThread autoreleased with no pool
in place - just leaking - break on objc_autoreleaseNoPool() to debug
I’m not familiar with the memory management of obj-C. Can someone explain this to me? Thanks a lot!
you have to create a NSAutoreleasePool for every thread that need invoke
autoreleaseinclude main thread