QPixmap memory leak issue

  • when I create QPixmap and call delete and check the memory allocation it show memory leak.
    Please find the code snippet below,

    _CrtMemState memAtStart;
    _CrtMemCheckpoint( &memAtStart );
    QPixmap *pix = new QPixmap(":/ActiveImage");
    delete pix;
    _CrtMemState memAtEnd
    _CrtMemCheckpoint( &memAtEnd );
    _CrtMemState memDiff;
    if ( _CrtMemDifference( &memDiff, &memAtStart, &memAtEnd)){
        _CrtMemDumpStatistics( &memDiff );
        FAIL()<< "memoryleak detected";

    Event I tried calling QPixmapCache:: remove() and clear() methods , However still it shows memory leak.

    Please let me know when the memory will get cleared.

    [EDIT: Updated code formatting --JKSH]

  • Lifetime Qt Champion


    deleting a piece allocated memory doesn't mean it will go back directly to the OS. You have an interesting article here about memory management

    By the way QPixmap is an implicitly shared class so you rarely need to allocate it on the heap

Log in to reply