简体   繁体   English

Mac OS X中的fsevents watcher中的内存泄漏

[英]Memory Leak in fsevents watcher in Mac OS X

I need file system notifications in Mac OS X and i'm reading from /dev/fsevents. 我需要Mac OS X中的文件系统通知,而且我正在从/ dev / fsevents中读取内容。 A Sample code of capturing fsevents in Mac OS X : http://www.codecollector.net/view/1066/raw_fsevents . 在Mac OS X中捕获fsevent的示例代码: http : //www.codecollector.net/view/1066/raw_fsevents In this code, you can see the buffer read from /dev/fsevents is processed as soon as it is read. 在此代码中,您可以看到从/ dev / fsevents读取的缓冲区一被读取就被处理。 But when i do that, events are missing due to the delay created in processing. 但是当我这样做时,由于在处理中创建的延迟而导致事件丢失。 So i have created a new char pointer and memcpy'd the buffer read from /dev/fsevents and added the new char* to a queue and processed the queue in a new thread. 因此,我创建了一个新的char指针,并从/ dev / fsevents中读取了缓冲区,并将新的char *添加到队列中,并在新线程中处理了该队列。 But when i process the char* like in 'print_event' & 'dump_entry', the char* pointer gets realligned and when i check for strlen() after processing, it says 0 or 1 bytes length only. 但是当我像在'print_event'和'dump_entry'中那样处理char *时,char *指针变为实数,并且当我在处理后检查strlen()时,它只说0或1个字节的长度。 So during processing, the memory is leaking. 因此在处理期间,内存正在泄漏。

Any idea how to delete the allocated char*, this is leaking more memory for more events. 任何想法如何删除分配的char *,这将泄漏更多事件的更多内存。 Please share your thoughts on this. 请分享您对此的想法。 Thanks in advance. 提前致谢。

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而不是使用为与其交互而设计的FSEvents接口(至少在最常见的情况下)? /dev/fsevents is pretty tricky to talk to directly. / dev / fsevents直接交谈非常棘手。 Ars Technica did a nice writeup. Ars Technica做的很好。

For this kind of code, I'd get away from malloc and memcpy. 对于这种代码,我将摆脱malloc和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. 这正是GCD旨在处理的问题,尤其是使用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. 您可以使dispatch_source读取/ dev / fsevents并将其传递回dispatch_data对象。 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." 还不清楚“ char *指针变为实数”的含义。 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. 但是除非您真的需要原始访问权限,否则我将查看FSEvents接口。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM