[SOLVED] Memory management question



  • Hi guys, I want to make sure that my code doesn't have any memory leak. i'm new to C++ and pointers.
    Here is my question (see commentary)
    Thank you!

    @subtitleActionGroup = new QActionGroup(this); //OK parent will delete this children when deleted

    QAction *actionSubtitleChoice;
    subtitleActionGroup->addAction(actionSubtitleChoice); // Will actionSubtitle be delete when subtitleActionGroup is deleted? If not, I will have to store an array of QAction and delete it, I just want to know if doing this work is useless@

    Full function code below:

    @//---- Construct subtitle menu
    if (subtitleActionGroup == nullptr) {

        qDebug() << "nullptr...";
    
        subtitleActionGroup = new QActionGroup(this);
        subtitleActionGroup->setExclusive(true);
        QAction *actionSubtitleChoice;
    
    
        int nbSub = _video->subtitleCount();
    
        int i=0;
        if (nbSub>0) {
            QStringList lstSub = _video->subtitleDescription();
            foreach (QString str, lstSub) {
                qDebug() << "ADDING Sub :" << str;
                actionSubtitleChoice = subMenuSubtitle->addAction(str);
                actionSubtitleChoice->setCheckable(true);
                actionSubtitleChoice->setData(i);
                subtitleActionGroup->addAction(actionSubtitleChoice);
                connect(actionSubtitleChoice, SIGNAL(triggered()), this, SLOT(subtitleChanged()));
                i++;
            }
        }@


  • I found the answer in the doc.

    Do not use .addAction on the QActionGroup.

    Use
    @void QAction::setActionGroup ( QActionGroup * group )@
    to add the Action to the ActionGroup

    then no memory management is needed..



  • Finally I add to use .addAction() because I need to add it to multiple group.

    I add to do memory management (see code below)
    Is there a way to set a pointer ton NULL like in Java?
    If I use "subtitleActionGroup = nullptr;" I get a runtime crash

    Thanks

    @void DemoPlayer::resetSubtitle() {
    // subtitleActionGroup->removeAction();

    foreach (QAction *myAction, lstSubAction) {
    
        qDebug() << "need to remove this action...";
    
        disconnect(myAction, SIGNAL(triggered()), this, SLOT(subtitleChanged()));
        subtitleActionGroup->removeAction(myAction);
        subMenuSubtitle->removeAction(myAction);
    

    // subtitleActionGroup = nullptr;
    delete myAction;
    }
    lstSubAction.clear();

    }@

    [EDIT] I'm dump I was doing this in the loop
    put "subtitleActionGroup = nullptr;" out and it worked



  • Just for information, you can also use Dr Memory to check for memory leakage in your application.



  • Hey Sam,

    I just used Dr.Memory

    The file generated is huge.
    The thing is, I don't really know how to interpret the memory leak error
    Thanks


    ERRORS FOUND:
    8 unique, 172 total unaddressable access(es)
    2278 unique, 52237 total uninitialized access(es)
    0 unique, 0 total invalid heap argument(s)
    0 unique, 0 total GDI usage error(s)
    0 unique, 0 total warning(s)
    26 unique, 51 total, 4024 byte(s) of leak(s)
    0 unique, 0 total, 0 byte(s) of possible leak(s)
    ERRORS IGNORED:
    3 potential error(s) (suspected false positives)
    (details: C:\Users\Toto\AppData\Roaming\Dr. Memory\DrMemory-PowerVelo2.exe.4816.000\potential_errors.txt)
    483 potential leak(s) (suspected false positives)
    (details: C:\Users\Toto\AppData\Roaming\Dr. Memory\DrMemory-PowerVelo2.exe.4816.000\potential_errors.txt)
    398 unique, 496 total, 117896 byte(s) of still-reachable allocation(s)
    (re-run with "-show_reachable" for details)
    Details: C:\Users\Toto\AppData\Roaming\Dr. Memory\DrMemory-PowerVelo2.exe.4816.000\results.txt

    Example of leak :

    Error #2290: LEAK 8 direct bytes 0x00da8eb0-0x00da8eb8 + 0 indirect bytes

    0 replace_RtlAllocateHeap [d:\drmemory_package\common\alloc_replace.c:3048]

    1 KERNELBASE.dll!LocalAlloc +0x5e (0x754458de <KERNELBASE.dll+0x158de>)

    2 libGLESv2.dll!glViewport +0x1d7 (0x5cef3698 <libGLESv2.dll+0x13698>)

    3 libGLESv2.dll!Ordinal178 +0x4f740 (0x5cf55541 <libGLESv2.dll+0x75541>)

    4 libGLESv2.dll!Ordinal178 +0x4f6b7 (0x5cf554b8 <libGLESv2.dll+0x754b8>)

    5 ntdll.dll!RtlQueryEnvironmentVariable +0x240 (0x779a99a0 <ntdll.dll+0x399a0>)

    6 ntdll.dll!RtlAllocateActivationContextStack +0x1ce (0x779aa1e6 <ntdll.dll+0x3a1e6>)

    7 ntdll.dll!RtlDecodePointer +0xf6 (0x779a9ecc <ntdll.dll+0x39ecc>)

    8 ntdll.dll!LdrInitializeThunk +0xf (0x779a9ef9 <ntdll.dll+0x39ef9>)

    Example of Leak 2
    Error #2301: LEAK 12 direct bytes 0x027b1b40-0x027b1b4c + 0 indirect bytes

    0 replace_calloc [d:\drmemory_package\common\alloc_replace.c:2310]

    1 libstream_filter_dash_plugin.dll!vlc_entry_license__1_2_0l+0x1917 (0x57135bd8 <libstream_filter_dash_plugin.dll+0x15bd8>)

    2 libstream_filter_dash_plugin.dll!vlc_entry_license__1_2_0l+0x7e866 (0x571b2b27 <libstream_filter_dash_plugin.dll+0x92b27>)

    3 libstream_filter_dash_plugin.dll!? +0x0 (0x571213fa <libstream_filter_dash_plugin.dll+0x13fa>)

    4 libstream_filter_dash_plugin.dll!? +0x0 (0x57121443 <libstream_filter_dash_plugin.dll+0x1443>)

    5 ntdll.dll!LdrResSearchResource +0xb4c (0x779ad939 <ntdll.dll+0x3d939>)

    6 ntdll.dll!LdrResSearchResource +0xa0f (0x779ad7fc <ntdll.dll+0x3d7fc>)

    7 ntdll.dll!LdrLoadDll +0x7a (0x779ac558 <ntdll.dll+0x3c558>)

    8 KERNELBASE.dll!LoadLibraryExW +0x1f0 (0x75442c95 <KERNELBASE.dll+0x12c95>)

    9 KERNEL32.dll!LoadLibraryW +0x10 (0x759748dc <KERNEL32.dll+0x148dc>)

    #10 libvlccore.dll!vlc_accept +0x2db (0x5942960c <libvlccore.dll+0x9960c>)
    #11 libvlccore.dll!module_config_free +0x3f (0x5940a040 <libvlccore.dll+0x7a040>)



  • The above list shows the stack sequence, you can increase the callstact frames which will give you where you have a memory leak, try running with the argument as

    • callstack_max_frames 50 // <------- 50 us the max no. of frames

    here you can check for the line numbers of your code like for my tests I get

    Error #17: LEAK 96 direct bytes 0x0378b288-0x0378b2e8 + 96 indirect bytes

    0 replace_operator_new [d:\drmemory_package\common\alloc_replace.c:2421]

    1 Uid1.1.dll!ui::mainframe::menu::LiMenuFileReader::parseXML_Folder [c:\qt-development\foundation\ui\pre-releases\1.1\mainframe\menu\limenufilereader.cpp:242]

    2 Uid1.1.dll!ui::mainframe::menu::LiMenuFileReader::parseXML_Folder [c:\qt-development\foundation\ui\pre-releases\1.1\mainframe\menu\limenufilereader.cpp:287]

    3 Uid1.1.dll!ui::mainframe::menu::LiMenuFileReader::parseXML_LisecInterface [c:\qt-development\foundation\ui\pre-releases\1.1\mainframe\menu\limenufilereader.cpp:154]

    4 Uid1.1.dll!ui::mainframe::menu::LiMenuFileReader::parseXML [c:\qt-development\foundation\ui\pre-releases\1.1\mainframe\menu\limenufilereader.cpp:111]

    5 Uid1.1.dll!ui::mainframe::menu::LiMenuFileReader::parseMenuFile [c:\qt-development\foundation\ui\pre-releases\1.1\mainframe\menu\limenufilereader.cpp:78]

    6 Uid1.1.dll!ui::mainframe::menu::LiMenuFileReader::parseMenuFile [c:\qt-development\foundation\ui\pre-releases\1.1\mainframe\menu\limenufilereader.cpp:62]

    7 Uid1.1.dll!ui::mainframe::menu::LiMenuFileReader::loadMenuFile [c:\qt-development\foundation\ui\pre-releases\1.1\mainframe\menu\limenufilereader.cpp:29]

    8 Uid1.1.dll!ui::mainframe::panel::LiMainFramePanel::LiMainFramePanel [c:\qt-development\foundation\ui\pre-releases\1.1\mainframe\panel\limainframepanel.cpp:92]

    9 Uid1.1.dll!ui::LiApplication::getMainFrame [c:\qt-development\foundation\ui\pre-releases\1.1\liapplication.cpp:319]

    #10 Uid1.1.dll!ui::LiApplication::getMainFramePanel [c:\qt-development\foundation\ui\pre-releases\1.1\liapplication.cpp:329]
    #11 main [c:\test\lialarmtype\main.cpp:14]

    For more information you can also look at the documentation e.g "Error Types Reported by Dr. Memory":http://drmemory.org/docs/page_types.html


Log in to reply
 

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