开发者

How to handle memory leaks?

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


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.


  1. 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.

  2. You can use microsoft's memory link detector embedded at VS. Find more in this link.


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.


Well, I have no experience in Qt Nokia SDK, but based on my C++ knowledge.

  1. Yes. Everything created by new should be later freed. A pointer cannot free itself and C++ doesn't offer any garbage collector.

  2. 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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜