我有一个 macruby
应用程序,它泄漏了 gigs 内存。 Ruby 用于逻辑内容并访问我的一些 Objective-C 类,访问 Accessibility-API 等功能并制作一些屏幕截图并通过 CIImage 处理它们。
有 ruby 和 Objective-C 代码,哪一个处理内存?
据我所知,ruby 会自动收集垃圾,但对于 Obj.-C,我必须使用一些 NSAutoreleasePool
。但是我应该在哪里释放
池呢?我的 ruby 代码需要处理返回值。
在我的项目设置中,我启用了垃圾收集(-fobjc-gc-only
)。
这是一些代码:
+ (CGSize) getSizeAttribute: (AXUIElementRef)element attribute:(CFStringRef) attribute { AXValueRef temp; CGSize point; AXError resultCode; resultCode = AXUIElementCopyAttributeValue( element, attribute, (CFTypeRef *)&temp ); AXValueGetValue(temp, kAXValueCGSizeType, &point); return point; }
最佳答案
在您的代码中,
resultCode = AXUIElementCopyAttributeValue( element, attribute, (CFTypeRef *)&temp );
temp 永远不会被释放。
您应该添加:
CFMakeCollectable(temp)
MacRuby 应用程序默认在 GC 模式下运行,但您的 CF/Obj-C 内存管理可能有点棘手(在 MacRuby 之外),请查看 Apple 开发站点上提供的文档。
关于ruby - 使用macruby和Objective-C时的内存管理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/830230/