如何处理内存泄漏?
我对内存处理不太熟悉,但我目前正在开发一个 Qt 项目(c++),使用 Qt Nokia SDK 为 Symbian 设备开发一个应用程序。
平台:Windows 7
第一个问题: 如果我创建一个像这样的按钮:
QPushButton *button = new QPushButton(parent);
我必须删除它吗? (我认为不是,因为它是用户界面的一部分,但如果我错了,请纠正我)。
第二个问题: 我怎样才能找到内存泄漏,你知道有什么好的程序可以帮助我解决这个问题吗?
我尝试过使用诺基亚分析器工具,但是当我运行工具时:
atool.exe -lf 构建armv5 udeb -fphoneMeomoryLog
我刚刚得到
构建类型:udeb 构建平台:armv5 数据采集方式:log to 文件分配调用堆栈大小:40 空闲调用堆栈大小:0 延迟 free:功能已禁用堆损坏检查(保护块):功能 禁用AnalyzeTool:错误,创建/读取makefile。
希望有人可以回答我。
提前致谢
I'm not that familiar to memory handling, but i am currently working on a Qt project (c++), developing an app for Symbian devices, using the Qt Nokia SDK.
Platform: Windows 7
1'st question:
If i create a pushbutton like this:
QPushButton *button = new QPushButton(parent);
Do i have to delete it? (I think no, since it is part of the UI, but correct me if i'm wrong).
2'nd question:
How can i find a memory leak, do you know of any good programs that can help me with this?
I've tried using Nokia Analyzer tool, but when i run atool:
atool.exe -lf build armv5 udeb -f phoneMeomoryLog
i just get
Build type: udeb Build platform: armv5 Data gathering mode: log to
file Allocation call stack size: 40 Free call stack size: 0 Deferred
free: feature disabled Heap corruption check (guard blocks): feature
disabled AnalyzeTool : Error, creating/reading makefiles.
Hope someone can answer me.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
第一个问题:不,你不必删除它。当父级被删除时,它也会被删除。因此,您必须删除可能是表单或对话框的父级。
当您使用 Qt::WA_DeleteOnClose 标志创建对话框时,Qt 会删除当小部件接受关闭事件时此小部件。所以你不会有任何内存泄漏。
因此,使用此标志创建对话框并像现在一样添加小部件,就可以了。
第二个问题:如果您在 Linux 上运行,请使用 valgrind。
示例:valgrind --tool=memcheck --leak-check=yes ./myprogramname
valgrind 有许多选项可用于微调。
如果您使用较旧的 C++ 或 QScopedPointer,还可以使用 *unique_ptr* 或 *auto_ptr*是避免内存泄漏的良好编程技术。
1'st question : No you don't have to delete it. It will be deleted when the parent is deleted. So you have to delete the parent which is probably a form or dialog.
When you create the dialog with Qt::WA_DeleteOnClose flag Qt deletes this widget when the widget has accepted the close event. So you will not have any memory leaks.
So create your dialog with this flag and add your widgets as you are doing now and you will be fine.
2'nd question: If you are running on linux use valgrind.
example : valgrind --tool=memcheck --leak-check=yes ./myprogramname
valgrind has many options you can use for fine tuning.
Also using *unique_ptr* or *auto_ptr* if you are using older c++ or QScopedPointer are good programming techniques to avoid memory leaks.
您的第一个问题的答案是否定的。似乎与 Windows 标准 GUI 对象不同,在 Qt 中您不应该删除它。当您的主窗口关闭时,它就会被释放。
您可以使用 VS 中嵌入的微软内存链接检测器。在此链接中查找更多信息。
The answer to your first Q is No. It seems that unlike windows standard GUI Objects, in Qt you should not delete it. It is being released when your main window is being closed.
You can use microsoft's memory link detector embedded at VS. Find more in this link.
两者“当您的主窗口关闭时,它就会被释放。”和“父级只是释放 UI 资源,而不是内存!!! – hsalimi”是错误的。父进程保存其子进程的列表,并在其自身被销毁时将其删除。这与“UI 资源”或“主窗口”无关,这是正常的 QObject 行为。 std::auto_ptr 在这里既不需要也没有用。
Both "It is being released when your main window is being closed." and "The parent just releases the UI resources, not the memory !!! – hsalimi " are wrong. The parent keeps a list of its children and will delete them when it is destructed itself. This has nothing to do with "UI resources" or "main window", it's normal QObject behaviour. std::auto_ptr is neither needed nor useful here either.
好吧,我没有 Qt Nokia SDK 的经验,但基于我的 C++ 知识。
是的。 new 创建的所有内容都应该稍后释放。指针无法释放自身,并且 C++ 不提供任何垃圾收集器。 是的
有几种方法。例如,检查您正在执行多少新建以及多少删除。对于每个新的内容,都应该在某处删除。检查使用的内存:如果使用的内存只增加而从不(或不经常)减少,那么您没有正确处理内存。确保删除析构函数上所有分配的资源。
为了避免这种情况,您可以使用自动指针
Well, I have no experience in Qt Nokia SDK, but based on my C++ knowledge.
Yes. Everything created by new should be later freed. A pointer cannot free itself and C++ doesn't offer any garbage collector.
There are several ways. For example, check how many new's you're doing and how many delete's. For each new there should be a delete somewhere. Check the memory used: if memory used only grows and never (or not often) decreases, then you're not handling memory correctly. Make sure you're deleting any allocated resources on destructors.
To avoid this you can use auto-pointers