Problem sending notification from C callback function

  • Greetings.

    I'm having a little problem due to lack of knowledge here.

    I have this callback from DiskArbritation framework
    and I would like to send a notification to my app.
    but it does not seem to work.

    the callback is registered like this.
    daSession  = DASessionCreate(kCFAllocatorSystemDefault);
    DASessionScheduleWithRunLoop(daSession, [[NSRunLoop currentRunLoop]
    getCFRunLoop], kCFRunLoopCommonModes);
    DARegisterDiskDisappearedCallback(daSession, NULL,
    &DiskDisappearedCallback, (void *)self);

    the callback is defined like this.
    void DiskDisappearedCallback(DADiskRef disk, void *context){
    CFDictionaryRef diskDescription = DADiskCopyDescription(disk);
    NSDictionary *nsDiskDescription = (NSDictionary *)diskDescription;

    NSString *name = [nsDiskDescription valueForKey:@"DAVolumeName"];
    NSString *type = [nsDiskDescription valueForKey:@"DAVolumeKind"];
    NSString *path = [nsDiskDescription valueForKey:@"DAVolumePath"];

    [[NSNotificationCenter defaultCenter]
    postNotificationName:ADD_REMOVE_NOTIFICATION object:nil userInfo:nil];
    }

    Obviously, default center does not exist in the callback, but i cant
    seem to typecast the context to
    my object type and retrieve the notification center from the object. i
    just do not know how to do it.

    could someone enlighten me please ?

    thank you Sandro Noel.
  • Sandro Noel (<sandro.noel...>) on 2009-01-08 8:57 PM said:

    > DARegisterDiskDisappearedCallback(daSession, NULL,
    > &DiskDisappearedCallback, (void *)self);

    Is your application garbage collected?  If so, passing self as the
    context could be problematic.  See:

    <http://developer.apple.com/documentation/Cocoa/Conceptual/
    GarbageCollection/Articles/gcUsing.html#//apple_ref/doc/uid/TP40008006-SW4
    >

    Sean
  • On Jan 8, 2009, at 7:57 PM, Sandro Noel wrote:

    > I have this callback from DiskArbritation framework
    > and I would like to send a notification to my app.
    > but it does not seem to work.

    Does not seem to work in what way?  Are you getting compiler errors or
    warnings?  Crashes at runtime?  What?

    > the callback is defined like this.
    > void DiskDisappearedCallback(DADiskRef disk, void *context){
    > CFDictionaryRef diskDescription = DADiskCopyDescription(disk);
    > NSDictionary *nsDiskDescription = (NSDictionary *)diskDescription;
    >
    > NSString *name = [nsDiskDescription valueForKey:@"DAVolumeName"];
    > NSString *type = [nsDiskDescription valueForKey:@"DAVolumeKind"];
    > NSString *path = [nsDiskDescription valueForKey:@"DAVolumePath"];
    >
    > [[NSNotificationCenter defaultCenter]
    > postNotificationName:ADD_REMOVE_NOTIFICATION object:nil userInfo:nil];
    > }
    >
    > Obviously, default center does not exist in the callback,

    Actually, I don't know what you mean here, so it's far from obvious.

    The default notification center is a singleton.  It always "exists" in
    the sense that you can always retrieve it using [NSNotificationCenter
    defaultCenter], just as you have.

    > but i cant seem to typecast the context to
    > my object type and retrieve the notification center from the object.
    > i just do not know how to do it.

    Well, since the default notification center is a singleton, it is
    effectively globally available.  You don't need to stash a reference
    to it in your object and retrieve it from there.

    With regard to the general question of how you can message the object
    passed in context: given that the context is an object pointer, you
    can just cast it:

    id myObject = (id)context;
    [myObject someMethod:someArgument];

    If you have a more specific type than id, go ahead and use that.

    Regards,
    Ken
  • Ken thank you.

    the problem was elsewhere in a dictionary key name...

    thank you for your help!!!

    Sandro Noel.

    On 9-Jan-09, at 2:09 AM, Ken Thomases wrote:

    > On Jan 8, 2009, at 7:57 PM, Sandro Noel wrote:
    >
    >> I have this callback from DiskArbritation framework
    >> and I would like to send a notification to my app.
    >> but it does not seem to work.
    >
    > Does not seem to work in what way?  Are you getting compiler errors
    > or warnings?  Crashes at runtime?  What?
    >
    >
    >> the callback is defined like this.
    >> void DiskDisappearedCallback(DADiskRef disk, void *context){
    >> CFDictionaryRef diskDescription = DADiskCopyDescription(disk);
    >> NSDictionary *nsDiskDescription = (NSDictionary *)diskDescription;
    >>
    >> NSString *name = [nsDiskDescription valueForKey:@"DAVolumeName"];
    >> NSString *type = [nsDiskDescription valueForKey:@"DAVolumeKind"];
    >> NSString *path = [nsDiskDescription valueForKey:@"DAVolumePath"];
    >>
    >> [[NSNotificationCenter defaultCenter]
    >> postNotificationName:ADD_REMOVE_NOTIFICATION object:nil
    >> userInfo:nil];
    >> }
    >>
    >> Obviously, default center does not exist in the callback,
    >
    > Actually, I don't know what you mean here, so it's far from obvious.
    >
    > The default notification center is a singleton.  It always "exists"
    > in the sense that you can always retrieve it using
    > [NSNotificationCenter defaultCenter], just as you have.
    >
    >
    >> but i cant seem to typecast the context to
    >> my object type and retrieve the notification center from the
    >> object. i just do not know how to do it.
    >
    > Well, since the default notification center is a singleton, it is
    > effectively globally available.  You don't need to stash a reference
    > to it in your object and retrieve it from there.
    >
    > With regard to the general question of how you can message the
    > object passed in context: given that the context is an object
    > pointer, you can just cast it:
    >
    > id myObject = (id)context;
    > [myObject someMethod:someArgument];
    >
    > If you have a more specific type than id, go ahead and use that.
    >
    > Regards,
    > Ken
    >