Results 1 to 5 of 5

Thread: Memory leak

  1. #1
    Join Date
    Dec 2009
    Location
    Shenzhen,China
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Memory leak

    I declare a global class variable, and it inherit from QObject, the constructor is like this:

    MyClass a;

    MyClass::MyClass(QObject *parent)
    : QObject(parent)
    {

    }

    The default parent is NULL, so when it's create the parent is set to NULL.
    The problem is that it will raise memory problem.
    Before a is created, it will call QObject's constructor, this is the constructor in the source code QObject.cpp.

    QObject::QObject(QObject *parent)
    : d_ptr(new QObjectPrivate)
    {
    Q_D(QObject);
    d_ptr->q_ptr = this;
    d->threadData = (parent && !parent->thread()) ? parent->d_func()->threadData : QThreadData::current();
    d->threadData->ref();
    QT_TRY {
    if (!check_parent_thread(parent, parent ? parent->d_func()->threadData : 0, d->threadData))
    parent = 0;
    setParent(parent);
    } QT_CATCH(...) {
    d->threadData->deref();
    QT_RETHROW;
    }
    qt_addObject(this);
    }

    QThreadData *QThreadData::current()
    {
    qt_create_tls();
    QThreadData *threadData = reinterpret_cast<QThreadData *>(TlsGetValue(qt_current_thread_data_tls_index)) ;
    if (!threadData) {
    QThread *adopted = 0;
    if (QInternal::activateCallbacks(QInternal::AdoptCurr entThread, (void **) &adopted)) {
    Q_ASSERT(adopted);
    threadData = QThreadData::get2(adopted);
    TlsSetValue(qt_current_thread_data_tls_index, threadData);
    adopted->d_func()->running = true;
    adopted->d_func()->finished = false;
    static_cast<QAdoptedThread *>(adopted)->init();
    } else {
    threadData = new QThreadData;
    // This needs to be called prior to new AdoptedThread() to
    // avoid recursion.
    TlsSetValue(qt_current_thread_data_tls_index, threadData);
    QT_TRY {
    threadData->thread = new QAdoptedThread(threadData);
    } QT_CATCH(...) {
    TlsSetValue(qt_current_thread_data_tls_index, 0);
    threadData->deref();
    threadData = 0;
    QT_RETHROW;
    }
    threadData->deref();
    }

    if (!QCoreApplicationPrivate::theMainThread) {
    QCoreApplicationPrivate::theMainThread = threadData->thread;
    } else {
    HANDLE realHandle = INVALID_HANDLE_VALUE;
    #if !defined(Q_OS_WINCE) || (defined(_WIN32_WCE) && (_WIN32_WCE>=0x600))
    DuplicateHandle(GetCurrentProcess(),
    GetCurrentThread(),
    GetCurrentProcess(),
    &realHandle,
    0,
    FALSE,
    DUPLICATE_SAME_ACCESS);
    #else
    realHandle = (HANDLE)GetCurrentThreadId();
    #endif
    qt_watch_adopted_thread(realHandle, threadData->thread);
    }
    }
    return threadData;
    }

    Because the parent is nil , so it will go to QThreadData::current(), because it's the first time to call QThreadData::current(), so it will jump to line "threadData = new QThreadData" to create threadData . the ref of d->threadData->ref will create the reference of threadData next time. that's the problem! because i found that the ref of threadData is not equal to 0 when the application exit. so it raise the memory leak.

    is it clear?

    Thanks appreicat for any advice!

  2. #2
    Join Date
    Mar 2007
    Posts
    57
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory leak

    If you would've checked the destructor of QObject you would've seen this:

    Qt Code:
    1. d->threadData->deref();
    To copy to clipboard, switch view to plain text mode 

    which should get to this:

    Qt Code:
    1. void QThreadData::deref()
    2. {
    3. #ifndef QT_NO_THREAD
    4. if (!_ref.deref())
    5. delete this;
    6. #endif
    7. }
    To copy to clipboard, switch view to plain text mode 

    In other words, it seems that you yourself are not deleting the object. Perhaps you should consider changing your global object (why have a global object in the first place? Maybe you could use a singleton instead?) with a pointer to this object. You can then easily delete it at the end of your program.

  3. #3
    Join Date
    Dec 2009
    Location
    Shenzhen,China
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Memory leak

    Yes, it will go to QThreadData::deref(), but the problem is that the _ref.deref is not equal to 0 when the application is going to exit.
    The object will call destructor by itself when the application exit.
    I will try to use a singleton.
    Thanks!

  4. #4
    Join Date
    Dec 2009
    Location
    Shenzhen,China
    Posts
    21
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: Memory leak

    And i find that there is the same problem with the demo of Qt. So can anyone tell me that wether is real memory leak or just the detect tool lies?
    Last edited by yxtx1984; 26th February 2010 at 09:00.

  5. #5
    Join Date
    Mar 2007
    Posts
    57
    Thanks
    2
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Memory leak

    Quote Originally Posted by yxtx1984 View Post
    Yes, it will go to QThreadData::deref(), but the problem is that the _ref.deref is not equal to 0 when the application is going to exit.
    The object will call destructor by itself when the application exit.
    I will try to use a singleton.
    Thanks!
    I don't get it. If _ref.deref != 0 QThreadData::deref should call "delete this", which seems to be what you want?

    Which tool are you using to check for memory leaks? Do you compile without optimization? Compile with (aggressive) optimization may give false results.

    If you still have problems, maybe you could supply a small program which exactly demonstrates the problem.

Similar Threads

  1. Qt dll + memory leak
    By Fastman in forum Qt Programming
    Replies: 3
    Last Post: 2nd August 2009, 13:28
  2. Memory leak of Qt?
    By Sheng in forum Qt Programming
    Replies: 1
    Last Post: 1st April 2009, 23:32
  3. Memory Leak in Qt
    By Krish_ng in forum Qt Programming
    Replies: 3
    Last Post: 22nd July 2007, 08:02
  4. Memory leak
    By vvbkumar in forum General Programming
    Replies: 4
    Last Post: 2nd September 2006, 15:31
  5. Memory leak
    By zlatko in forum Qt Programming
    Replies: 8
    Last Post: 28th March 2006, 19:02

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.