Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Memory Corruption: Qt allocates insufficient memory for my object
Forum Update on Monday, May 27th 2025

Memory Corruption: Qt allocates insufficient memory for my object

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    abhijeetmhatre
    wrote on 28 Sept 2013, 05:20 last edited by
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 28 Sept 2013, 20:42 last edited by
      #2

      Hi and welcome to devnet,

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

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • A Offline
        A Offline
        abhijeetmhatre
        wrote on 29 Sept 2013, 04:20 last edited by
        #3

        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

        1 Reply Last reply
        0
        • M Offline
          M Offline
          mmoll
          wrote on 30 Sept 2013, 10:32 last edited by
          #4

          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.

          1 Reply Last reply
          0

          1/4

          28 Sept 2013, 05:20

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved