Qt程序是不是会内存泄漏



  • 最近在Qt项目中发现,程序运行内存越来越大,然后使用_CrtDumpMemoryLeaks进行内存检查,发现很多泄漏提示,但是检查代码没发现问题.

    于是使用VS2010 创建了一个Qt console程序,代码如下,

    #include <QtCore/QCoreApplication>
    #include <assert.h>
    /************************************************************************/
    #ifdef _DEBUG
    #define _CRTDBG_MAP_ALLOC
    #include <crtdbg.h>
    #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, FILE, LINE)
    #define new DEBUG_CLIENTBLOCK
    static void Exit()
    {
    int i = _CrtDumpMemoryLeaks();
    assert( i == 0);
    }
    #define CheckMemoryLeaks() {atexit(Exit);}

    #else
    #define DEBUG_CLIENTBLOCK
    #define CheckMemoryLeaks() ;
    #endif

    /************************************************************************/
    int main(int argc, char *argv[])
    {
    CheckMemoryLeaks();
    QCoreApplication a(argc, argv);

    return a.exec();
    }

    ===============================================================================
    输出信息如下:

    Detected memory leaks!
    Dumping objects ->
    {176} normal block at 0x013A57F0, 12 bytes long.
    Data: < L ]L ]> 00 00 00 00 4C CF 1C 5D 4C CF 1C 5D
    {175} normal block at 0x013A57A8, 12 bytes long.
    Data: < L ]L ]> 00 00 00 00 4C CF 1C 5D 4C CF 1C 5D
    {174} normal block at 0x013A56F8, 112 bytes long.
    Data: < ] V: L ]> 8C CB 1B 5D B0 56 3A 01 00 00 00 00 4C CF 1C 5D
    {173} normal block at 0x013A56B0, 8 bytes long.
    Data: < ] V: > 98 CB 1B 5D F8 56 3A 01
    {172} normal block at 0x013A5670, 4 bytes long.
    Data: <L ]> 4C CF 1C 5D
    {171} normal block at 0x013A5620, 20 bytes long.
    Data: <@ ]@ ]@ ]@ ]> 40 20 1C 5D 40 20 1C 5D 40 20 1C 5D 40 20 1C 5D
    {170} normal block at 0x013A55E0, 4 bytes long.
    Data: <@ ]> 40 20 1C 5D
    {156} normal block at 0x013A5530, 16 bytes long.
    Data: < > 01 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    {155} normal block at 0x013A54F0, 4 bytes long.
    Data: <0U: > 30 55 3A 01
    {154} normal block at 0x013A5288, 552 bytes long.
    Data: < R: > 83 00 00 00 83 00 00 00 98 52 3A 01 CD CD CD CD
    {151} normal block at 0x013A50D8, 108 bytes long.
    Data: < ] P: L ]> E8 B9 1B 5D 90 50 3A 01 00 00 00 00 4C CF 1C 5D
    {150} normal block at 0x013A5090, 8 bytes long.
    Data: < ] P: > A8 B9 1B 5D D8 50 3A 01
    {149} normal block at 0x013A5018, 60 bytes long.
    Data: < P: > 02 00 00 00 90 50 3A 01 FC 1B 00 00 00 CD CD CD
    {145} normal block at 0x013A4E58, 32 bytes long.
    Data: < > 01 00 00 00 04 00 00 00 00 00 00 00 01 00 00 00
    {144} normal block at 0x013A4E08, 20 bytes long.
    Data: < 4)] \5] \5]( 1]> BC 34 29 5D F0 5C 35 5D 90 5C 35 5D 28 82 31 5D
    {143} normal block at 0x013A4DC8, 4 bytes long.
    Data: <XN: > 58 4E 3A 01
    {142} normal block at 0x013A4D78, 16 bytes long.
    Data: < > 01 CD CD CD 00 00 00 00 00 00 00 00 00 00 00 00
    {141} normal block at 0x013A4D38, 4 bytes long.
    Data: <xM: > 78 4D 3A 01
    Object dump complete.

    是方法错误还是Qt本身的问题?坐等高手````



  • 你的工具是用于检测MFC的,QT有自己的垃圾回收机制,就是对象父子关系。



  • QT的回收机制在哪里??我不知道你在哪里看到的回收机制,反正我是没有看到过。
    QT只是一个庞大的C++类库,它本身没有对C++有任何的改变,我是说new,delete,他自然也不会有回收机制,你依然要在你程序里去控制开辟出来的内存。



  • 一楼的意思是Qt在进入QObject的析构函数的时候,他会释放掉QObject内部所持有所有孩子的实例。这一点要格外注意。
    你的项目大,所以在设计的时候必须考虑:在事件循环的时候有没有用到new操作符。要么可以重载全局的new操作符,输出实例的个数。


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.