上下文指针在 FSEventStreamCreate 回调 (c++) 中丢失
Context Pointer Lost in FSEventStreamCreate Callback (c++)
im 尝试使用 FSEvent API 在 mac 中测试一个简单的文件观察程序。我收到了事件的回调,一切似乎都正常,但我无法恢复上下文结构的信息字段中包含的信息。这是我的代码:
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
那么,我做错了什么?提前感谢!
该问题与此代码无关。指针工作正常,但他的对象丢失了。
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 为什么使用 "this" 指针调用派生成员函数?
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用指针从C++中的数组中获取最大值
- 助记符和指向成员语法的指针
- 嵌入方指针压缩已禁用
- 数组的指针从不分段故障
- C++ 指针的内存地址和指向数组的内存地址如何相同?
- 何时在引用或唯一指针上使用移动语义
- QMetaObject invokeMethod的基于函数指针的语法
- 如何从 std::atomic 中提取指针 T<T>?
- 如何在 C# 中映射双 C 结构指针?
- C++将浮点指针值舍入为小数位数
- 为什么++(*p)更改指针值
- 调整大小后指向元素值的指针unordered_map有效?
- 正在将指针转换为范围
- 使用指向成员的指针将成员函数作为参数传递
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错