我正在尝试使用 FSEvent Api 在 mac 中测试一个简单的文件观察器。我得到了事件的回调,一切似乎都正常,但我无法恢复上下文结构的 info 字段中包含的信息。这是我的代码:
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();
}
我在回调中收到 sigsev 错误。此外,如果我在上下文中仅插入一个 int,例如:
int testInt = 4;
FSEventStreamContext context = {0, &testInt, NULL, NULL, NULL};
在回调中
qDebug() << *((int*)pClientCallBackInfo);
打印我 -1
那么,我做错了什么?提前谢谢!