NSDate
我有内存泄漏。请查看以下代码。
-(void)myMethods:(NSDate *)currentTime{
[NSThread detachNewThreadSelector:@selector(mySecondMethods) toTarget:self withObject:nil];
}
-(void) mySecondMethods{
NSDate * currentDateTime = [NSDate date];------->memory leak here
for (Event * event in array) {
if(![event checkOccur:currentDateTime]){
return;
} else {
[NSThread detachNewThreadSelector:@selector(start) toTarget:event withObject:nil];
}
}
}
答案 0 :(得分:7)
如果要调用线程中的任何方法,则必须使用自动释放池。
-(void) mySecondMethods
{
NSAutoReleasePool *pool = [[NSAutoReleasePool alloc] init];
NSDate * currentDateTime = [NSDate date];
for (Event * event in array) {
if(![event checkOccur:currentDateTime])
{
return;
}
else{
[NSThread detachNewThreadSelector:@selector(start) toTarget:event withObject:nil];
}
[pool drain];
}
答案 1 :(得分:0)
-(void) mySecondMethods{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDate * currentDateTime = [NSDate date];
for (Event * event in array) {
if(![event checkOccur:currentDateTime]){
return;
}
else{
[NSThread detachNewThreadSelector:@selector(start) toTarget:event withObject:nil];
}
}
[pool release];
}