0

我需要 Mac OS X 中的文件系统通知,我正在从 /dev/fsevents 读取。在 Mac OS X 中捕获 fsevents 的示例代码:http: //www.codecollector.net/view/1066/raw_fsevents。在这段代码中,您可以看到从 /dev/fsevents 读取的缓冲区一经读取就被处理。但是当我这样做时,由于处理过程中产生的延迟,事件会丢失。所以我创建了一个新的 char 指针并 memcpy'd 从 /dev/fsevents 读取的缓冲区,并将新的 char* 添加到队列并在新线程中处理队列。但是当我像在'print_event'和'dump_entry'中处理char *时,char *指针会重新对齐,并且当我在处理后检查strlen()时,它只显示0或1字节长度。所以在处理过程中,内存正在泄漏。

知道如何删除分配的 char*,这会为更多事件泄漏更多内存。请分享您对此的看法。提前致谢。

4

1 回答 1

0

Just curious: any reason you manually processing /dev/fsevents rather than using the FSEvents interface that's designed for interacting with it (at least for the most common cases)? /dev/fsevents is pretty tricky to talk to directly. Ars Technica did a nice writeup.

For this kind of code, I'd get away from malloc and memcpy. That's going to add a lot of overhead to your queue management. And I'd get away from manual thread management. This is exactly the kind of problem that GCD is designed to handle, in particular using dispatch_data. It lets you create immutable memory blocks that you can process and manipulate without requiring copying. You can make a dispatch_source to read /dev/fsevents and pass you back dispatch_data objects. See the Reading Data from a Descriptor section of the Concurrency Programming Guide for an example.

It's not quite clear what you mean by "the char* pointer gets realligned." Do you mean you're modifying the same variable on two threads without locks?

But unless you really need raw access, I'd be looking at the FSEvents interface.

于 2014-02-13T17:25:43.850 回答