我有记忆问题。
我有一个 C++ 库(来自 Eyescale 的均衡器),他们使用 Traversal Visitor Pattern 允许您向他们的类添加新功能。
我终于弄清楚了它是如何工作的,并且我有一个仅返回其中一个对象的属性的访问者。(因为我不知道它们是如何分配的)。
所以。
我的小代码是这样做的:
VisitorResult AGLContextVisitor::visit( Channel* channel )
{
// Search through Nodes, Pipes until we get to the right window.
// Add some code to make sure we find the right one?
// Not executing the following code as C++ in gdb?
eq::Window* w = channel->getWindow();
OSWindow* osw = w->getOSWindow();
AGLWindow* aw = (AGLWindow *)osw;
AGLContext agl_ctx = aw->getAGLContext();
this->setContext(agl_ctx);
return TRAVERSE_PRUNE;
}
所以这就是问题所在。
eq::Window* w = channel->getWindow();
(gdb) print w
0x0
但是如果我这样做:
(gdb) set objc-non-blocking-mode off
(gdb) print w=channel->getWindow()
0x300effb9
// 一个真实的内存位置,并设置 w 在 XCode 的调试器窗口中验证。
它对 osw 做同样的事情。
我不明白。为什么某些东西在(gdb)中起作用但在代码中不起作用?
该文件完全是一个 cpp 文件,但它似乎在 objc++ 中运行,因为我需要关闭阻塞。
帮助!?我觉得我在这里缺少一些内存管理基本的东西,无论是使用 C++ 还是 Obj-C。
[编辑]
channel->getWindow() 应该这样做:
/** @return the parent window. @version 1.0 */
Window* getWindow() { return _window; }
如果我从仅 C++ 的应用程序运行该代码,它也可以正常执行。
[编辑]
不...我尝试创建一个简单的独立程序,因为我厌倦了将它作为插件运行。调试很麻烦。
不,它也不能在 C++ 程序中运行。所以我真的不知道我做错了什么。
谢谢,
——斯蒂芬·弗拉尼