Cocoa in the Shell

About DADiskMountApprovalCallback double callback...

Some time ago I made a post on an issue I was having with Disk Arbitration. I was registering to DADiskMountApprovalCallback to detect when an disk image was mounted, but each time the callback was called twice, so I did a little hack to ignore the second call.

A few days ago, Aaron Burghardt posted a comment to say that my code contained a possible race condition and that I should instead register to DADiskDescriptionChangedCallback with the kDADiskDescriptionWatchVolumePath parameter.

So I tried what he said, and it works perfectly well, so I want to share the code here.

void description_changed(DADiskRef disk, CFArrayRef keys, void* ctx)
{
    NSDictionary* dic = DADiskCopyDescription(disk);
    // Do stuff
    [dic release];
}
// Code
DASessionRef session = DASessionCreate(kCFAllocatorDefault);
DARegisterDiskDescriptionChangedCallback(session, NULL, kDADiskDescriptionWatchVolumePath, description_changed, NULL);
// Code

In the end, it’s a win-win, because it’s less code and it’s safer.

So, a big thank to Aaron Burghardt for suggesting me this method.