Results 1 to 11 of 11

Thread: Memory leak weirdness

  1. #1
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Memory leak weirdness

    I'm using Wu Yongwei's memory leak detector (http://wyw.dcweb.cn/leakage.htm) and I think I've managed to get rid of any memory leaks, however I'm getting this weird error:

    Qt Code:
    1. delete: invalid pointer 040C7D78 (00409C91)
    2. *** Checking for memory corruption: START
    3. Heap data corrupt near 040C7D78 (size 124, 00403420)
    4. *** Checking for memory corruption: 1 FOUND
    To copy to clipboard, switch view to plain text mode 

    It looks like I'm trying to delete a pointer pointing to some memory that isn't allocated anymore? The program doesn't crash though.

    Of course the problem is that I get an address where this is occurring and digging through the ASM to find where this may be originating from just goes a little too far I'm afraid . The weird thing is that when I still had memory leaks, the output of the memory leak tool would also give addresses, instead of filenames & lines as shown on the website.

    Has anyone got a solution for this?

  2. #2
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Memory leak weirdness

    Did you compile the app in debug mode?
    make clean
    qmake -config debug
    make
    PS. You'll need debug version of Qt as well (notice that's there's a link for building debug libs in Start-menu).
    J-P Nurmi

  3. #3
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory leak weirdness

    I compiled in debug mode yes. As far as Qt goes, I compiled it from the source yesterday and I just checked and it seems debugging is on by default when running configure & make, so I'm guessing that isn't the problem either.

    Concerning enabling debugging stuff, my .pro file contains:
    QT += debug
    QMAKE_CXXFLAGS_DEBUG += -pg
    QMAKE_LFLAGS_DEBUG += -pg

  4. #4
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory leak weirdness

    Hm, k, I found the line that caused the illegal pointer error. But now I'm looking at 32 memory leaks and I have no idea where to look, because I'm getting addresses instead of files. Has anyone tried using this memory leak tool in Windows? Did it work as advertised on the website? If so, how did you implement it?

    * Edit: Ok, managed to fix it. Some mistunderstanding on my part. I knew I had to load the debug_new.h before system headers, but I didn't think Qt headers counted as system headers.
    Last edited by Darhuuk; 10th January 2008 at 18:24.

  5. #5
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory leak weirdness

    Ah well, while we're at it. I get odd results like this:
    Leaked object at 003DE248 (size 20, GUI\MainWindow.cpp:83)
    Leaked object at 0400C538 (size 20, GUI\MainWindow.cpp:88)
    Leaked object at 003DFE78 (size 16, GUI\MainWindow.cpp:93)
    Leaked object at 003DF750 (size 20, GUI\MainWindow.cpp:98)
    Leaked object at 040218C8 (size 20, GUI\MainWindow.cpp:107)
    Those lines are:
    centralwidget = new QWidget(this);
    gridLayoutContainer = new QWidget(centralwidget);
    gridLayout = new QGridLayout(gridLayoutContainer);
    lblStatusBar = new QLabel(centralwidget);
    lblEmpty = new QLabel(centralwidget);
    All of those variable are declared in my header file as private pointers.

    In my destructor ~MainWindow, I have:
    delete lblStatusBar;
    delete lblEmpty;
    delete gridLayout;
    delete gridLayoutContainer;
    delete centralwidget;
    As far as I can see, on destruction of the object, I delete every one of the objects that are the leaks according to the tool. The ~MainWindow gets called automatically when I do "delete mainWindowInstance;" right? Do I really have memory leaks or is the memory leak tool for some reason unable to handle this?

  6. #6
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Memory leak weirdness

    In fact, you wouldn't even need those deletes since every QObject automatically deletes its children upon destruction. Does the main window itself get properly destructed? Yeah, could be that the tool is not able to handle Qt that well after all..
    J-P Nurmi

  7. #7
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory leak weirdness

    Oh, well, then it's definitely the tool. Ok, time to delete some "delete" stuff .

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Memory leak weirdness

    Just to assure, in main() you do either
    • allocate the main window object on the stack
    • allocate the main window object on the heap and delete it by hand
    • allocate the main window object on the heap and set attribute Qt::WA_DeleteOnClose

    right?
    J-P Nurmi

  9. #9
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory leak weirdness

    The main window object is on the heap, the QApplication on the stack, but there shouldn't be a leak there or the tool would say so (well, maybe not, it's obviously not perfect). Here's the stripped down code from my main:
    Qt Code:
    1. int main(int argc, char *argv[])
    2. {
    3. QApplication app(argc, argv);
    4. MainWindow *diag = new MainWindow();
    5. diag->show();
    6. int iReturn = app.exec();
    7. delete diag;
    8. return iReturn;
    9. }
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Memory leak weirdness

    Alright good, so it falls to the second option. Btw, you could just allocate it on the stack as well, couldn't you?
    J-P Nurmi

  11. #11
    Join Date
    Jan 2008
    Posts
    32
    Thanks
    10
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Memory leak weirdness

    Yeah, true. I think I stole the basic code from some tutorial and just went with it. I've only been programming in C++ for 1 week. I thought it would be harder. Ok, I have some memory management to do, for other than that, it all comes down to the same stuff as Java, PHP, ... Thx for the help & assuring me the tool it at fault .

Similar Threads

  1. Memory leak detection
    By Sid in forum Qt Programming
    Replies: 8
    Last Post: 4th May 2011, 23:38
  2. Memory leak?
    By Enygma in forum Qt Programming
    Replies: 10
    Last Post: 4th September 2007, 17:24
  3. QPixMap and Memory leak
    By Krish_ng in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2007, 15:18
  4. Memory Leak in my Application :-(
    By Svaths in forum Qt Programming
    Replies: 4
    Last Post: 27th July 2007, 20:42
  5. Qt 4.1 Memory Leak
    By blackliteon in forum Qt Programming
    Replies: 14
    Last Post: 10th February 2006, 13:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.