Problems with memory allocation in classes

This is a discussion on Problems with memory allocation in classes within the C++ Programming forums, part of the General Programming Boards category; Hi! I have been writing several games in c++ lately and each time I encounter the same problem. I have ...

  1. #1
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190

    Question Problems with memory allocation in classes

    Hi!

    I have been writing several games in c++ lately and each time I encounter the same problem. I have a quite advance class structure with inheritance. I have several classes which include pointer arrays of other complex classes (which in their turn also have pointer arrays of other classes).

    Now everything seems to be correctly initialized and memory allocation seems ok, but sometimes my program crashes without any reason at the same spot. I have tried to debug it many times, but everything seems fine. But somehow I suspect that the classes included within another class overlap in memory. Is that possible? And if so, what can I do to avoid it?

    Here is another strange thing. I have a following class (much simplified):

    Code:
    class MyClass
    {
    	MyClass();
    	~MyClass();
    	
    	MyClass1 *A;
    	MyClass2 *B;
    	MyClass3 *C;
    }
    
    MyClass::MyClass()
    {
    	A=new MyClass1();
    	B=new MyClass2();
    	C=new MyClass3();
    }
    Now when I try to do something like C->myFunc(); I get SIGSEGV. When I debug the program, it says that when entering the function MyClass3::myFunc() other contents of class C is unavailable (this==NULL). That is why the function myFunc() can't access any of these. This is though I have clearly allocated memory for this class.

    If I now change the constructor of MyClass a bit:

    Code:
    MyClass::MyClass()
    {
    	C=new MyClass3(); //this comes now first
    	A=new MyClass1();
    	B=new MyClass2();
    }
    This works perfectly.

    Can anyone tell me what's going on here?

    Thanx
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,807
    Well based on what you are saying this should cause a crash but it doesn't for me
    Code:
    #include <iostream>
    
    class myClass1
    {
      public:
        void print()
        {     
           std::cout<<"myClass1 print()"<<std::endl;      
        }
    };
    
    class myClass2
    {
      public:
        void print()
        {
          std::cout<<"myClass2 print()"<<std::endl;      
        }
    };
    
    class myClass3
    {
      public:
        void print()
        {
          std::cout<<"myClass3 print()"<<std::endl;
        }
    };
    
    class myClass
    {
      public:
        myClass()
        {
          a = new myClass1;
          b = new myClass2;
          c = new myClass3;
        }
        void printAll()
        {
          a->print();
          b->print();
          c->print();
        }    
        ~myClass()
        {
          delete a;
          delete b;
          delete c;
        }  
      private:
        myClass1 *a;
        myClass2 *b;
        myClass3 *c;
    };
    
    int main(void)
    {  
      myClass mainTest;
      mainTest.printAll();
      std::cin.get();  
      return 0;
    }
    Maybe check what you are doing with your allocation.
    Last edited by prog-bman; 01-08-2005 at 04:37 AM.
    Woop?

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    try to post an example that does crash.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    Ok, here it is: http://www.home.no/masterofthewind/m...l-0.0.1.tar.gz

    this is the source of the whole game. I'm really sorry it's messy and poor commented.

    And: it uses sdl and opengl. it compiles fine on my linux machine. i don't think it will be any problem to compile on win either


    now in the constructor "MapEdit::MapEdit(GLCamera *nCamera, SDL_Event *nSDLEventQueue, GLTexturePool *nTexturePool)" this command : "EditMap=new Map();" comes after "cout<<"Initializing map edit mode...";". the program crashes after you start it and press 'e'.

    but if you put "EditMap=new Map();" after "refreshState();" the program crashes earlier.
    Last edited by MathFan; 01-08-2005 at 07:42 AM.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  5. #5
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    >> but if you put "EditMap=new Map();" after "refreshState();" the program crashes earlier.

    that's because refreshState() calls EditMap->resetPlane() before EditMap points to valid memory.

    as far as your original question goes, I can't see right off what the error might be and I'm not going to install OpenGL so that I can recompile the entire program and debug it. I would recommend that you code a set of 'dummy' classes that model the interactions of the objects in your program and see if you can duplicate the error and then post it here. also, try to compile the program without the makefile to make sure this isn't some kind of vtable invalidation problem.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  6. #6
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    that's because refreshState() calls EditMap->resetPlane() before EditMap points to valid memory.
    oops!

    ok, thanks a lot, Sebastiani! I'll try to recompile it manually. And if that doesn't help, I will post the code without opengl and sdl as soon as I can
    Last edited by MathFan; 01-08-2005 at 10:18 AM.
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

  7. #7
    Registered User MathFan's Avatar
    Join Date
    Apr 2002
    Posts
    190
    hm... I recompiled it, and the problem was still there

    but I have rewritten some of the classes and now it works more or less fine. I still don't know what was wrong, but anyway it's not there anymore!
    The OS requirements were Windows Vista Ultimate or better, so we used Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. POSIX Threads and dynamic memory allocation
    By PING in forum Linux Programming
    Replies: 1
    Last Post: 04-02-2009, 10:28 AM
  2. Increasing memory allocation in function
    By Ramses800 in forum C Programming
    Replies: 3
    Last Post: 12-16-2008, 04:30 AM
  3. To find the memory leaks without using any tools
    By asadullah in forum C Programming
    Replies: 2
    Last Post: 05-12-2008, 07:54 AM
  4. Question regarding Memory Leak
    By clegs in forum C++ Programming
    Replies: 29
    Last Post: 12-07-2007, 12:57 AM
  5. memory allocation for functions! :(
    By rahat in forum C Programming
    Replies: 4
    Last Post: 11-04-2001, 10:27 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21