Memory Corruption: Qt allocates insufficient memory for my object



  • Hi

    I am facing this problem since quite some time now.

    This is my class hierarchy
    QObject <- class1 <- class2

    I instantiate class2 using new and initialize the members.in its constructor.

    When I run my code through valgrind. IT shows invalid write error where I initialize the last member variable. Int, or char. It says that the address of that integer lies beyond the memory block that has been allocated for the object of class2.

    This problem occurs in Linux but not on Windows.
    Compilation warning are limited to "unused variables" and "Inherited classes have non virtual destructors".

    Has anyone encountered similar problems ?

    Regards
    Abhijeet


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    It's just curiosity, how do you know it doesn't happen on Windows ?



  • I did the following:

    printing address of the class; lets call it p1
    printing size of the class: s1
    printing address of the last attribute of the class; LA1

    In Linux: p1 + s1 < LA1
    in Windows: p1 + s1 > LA1

    .... putting some code to make it somewhat unambiguous....

    class2* c2p = new class2;

    printf ( "c2p=%d\n", c2p );
    printf ("sizeof class2=%d", sizeof (*c2p) );

    and in constructor of class2 I put a printf
    printf ( "end int ptr=%d\n", &attr1 );

    ....................................................................
    Sometime back I had devised workaround to get rid of this problem. This involved putting moving the code of the class from .cpp to .h.
    The problem goes away when I do this. Need to verify this again.
    But more than the solving the problem I want to know why this happens.
    ...................................................................................
    secondly I did the following
    I isolated class2 from its base class by modifying it so that it remains a pure
    C++ class. Still the corruption persists in a different way. Now it says that it is an invalid write inside the block allocated for the class

    I made the class very simple with just one const desctr and one member variable but .... problem persists

    I moved the class to different files .... problem persists
    I moved the class instantiation to just after main.... problem persists

    I made the class into separate application ... problem goes away.

    ............................................................................................

    I made the following conclusions.

    Somehow the compiler recognizes the last attribute of the class as being separate from the class.

    This is because of the structure of rest of the program and not the class itself

    ..........................................................

    My next step is to start cutting code from the rest of the program and check at what point does the leak go away.
    let me know if his is the right way to proceed or are there any other methods



  • If I may hazard a guess:

    The fact that moving code into the header file helps suggests that your class definition looks different to different pieces of the code.
    Is it possible that your class definition depends on some debug macro and that that macro is not set consistently across translation units?

    If you have something like:

    class2.h:
    @class Class2 {
    #ifdef MYDEBUGFLAG
    int debug;
    #endif
    int x;
    int y;
    Class2();
    int f() const;
    };@

    class2.cpp:
    @ #define MYDEBUGFLAG 1
    #include "class2.h"
    Class2::Class2() : debug(1), x(40), y(2) {}
    int Class2::f() const { return x + y; }@

    main.cpp:
    @ #include "class2.h"
    int main()
    {
    Class2 *c = new Class2(); // may crash
    int retval = c->f();
    delete c;
    return retval;
    }@

    that would probably explain your problems.


Log in to reply
 

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