我试图使用FSEvent Api在mac中测试一个简单的filewatcher。 我得到了事件的回调,一切似乎都有效但我无法恢复上下文结构的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
那么,我做错了什么? Thx提前!
答案 0 :(得分:0)
问题与此代码无关。指针工作正常,但他的对象丢失了。