Context Pointer Lost in FSEventStreamCreate Callback (c++)

314 views Asked by At

im trying to test a simple filewatcher in mac using the FSEvent Api. I get the callback with the events and everything seems to work but i can't recover the info contained in the info field of the context structure. Here is my code:

void feCallback(ConstFSEventStreamRef streamRef,
                   void* pClientCallBackInfo,
                   size_t numEvents,
                   void* pEventPaths,
                   const FSEventStreamEventFlags eventFlags[],
                   const FSEventStreamEventId eventIds[]);

Watcher::Watcher(const QString& pathToFolder) :
folderPath_(pathToFolder)
{
    CFStringCreateWithCString(kCFAllocatorDefault,path,kCFStringEncodingUTF8);
    NSString *directory = [[NSString stringWithUTF8String:folderPath_.toStdString().c_str()] stringByDeletingLastPathComponent];
    NSArray *pathToWatch = [NSArray arrayWithObjects:directory, nil];
    FSEventStreamContext context = {0, (void*)this, NULL, NULL, NULL}; //the second parameter is the 'user info' field

    CFAbsoluteTime latency = 3.0;
    FSEventStreamRef stream;
    stream = FSEventStreamCreate(NULL,
                             &feCallback,
                             &context,
                             (CFArrayRef)pathsToWatch,
                             kFSEventStreamEventIdSinceNow, 
                             latency,
                             kFSEventStreamCreateFlagFileEvents | kFSEventStreamCreateFlagNoDefer);
   if (!stream)
   {
       qDebug() << "Could not create event stream for path";
       return;
   }
   else
   {
       FSEventStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
       FSEventStreamStart(stream);
       qDebug() << "stream created";
   }
}

QString Watcher::getFolderPath()
{
    return folderPath_;
}

void  feCallback(ConstFSEventStreamRef streamRef,
                   void* pClientCallBackInfo,
                   size_t numEvents,
                   void* pEventPaths,
                   const FSEventStreamEventFlags eventFlags[],
                   const FSEventStreamEventId eventIds[])
{
    Q_UNUSED(streamRef)
    Q_UNUSED(eventIds)

    qDebug() << ((Watcher*)pClientCallBackInfo)->getFolderPath();
}

I get a sigsev error in the callback. Furthermore if i insert in the context just an int, for example:

int testInt = 4;
FSEventStreamContext context = {0, &testInt, NULL, NULL, NULL};

In the callback

qDebug() << *((int*)pClientCallBackInfo);

Prints me -1

So, what am i doing wrong? Thx in advance!

1

There are 1 answers

0
Sommerwild On BEST ANSWER

The problem was unrelated to this code. The pointer works fine but his object was lost.