I have a task that uses a CFRunLoopObserver like the following.
class Task {
CFRunLoopObserverRef fObserver;
public:
Task() {
CFRunLoopObserverContext context = {0, this, NULL, NULL, NULL};
fObserver = CFRunLoopObserverCreate(kCFAllocatorDefault,
kCFRunLoopBeforeWaiting,
true, 0, &MainRunLoopObserverCallback, &context);
::CFRunLoopAddObserver(CFRunLoopGetMain(), fObserver, kCFRunLoopCommonModes);
}
static void MainRunLoopObserverCallback(CFRunLoopObserverRef observer, CFRunLoopActivity activity, void *info) {
Task* task = reinterpret_cast<Task*>(info);
// task->member causes a crash if task deleted
}
~Task() {
if (fObserver)
{
::CFRunLoopRemoveObserver(CFRunLoopGetMain(), fObserver, kCFRunLoopCommonModes);
::CFRelease(fObserver);
}
}
};
I have noticed that the app crashes sometimes when the task object gets deleted before the CFRunLoopObserverCallBack is completed.
Would it be possible to make sure that the observer callback is complete before the observer is removed from the runloop?