Memory is changing on me!

This is a discussion on Memory is changing on me! within the C++ Programming forums, part of the General Programming Boards category; Hey all, I've been working on this project for awhile and after stepping through the debugger, I noticed why it ...

  1. #1
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215

    Memory is changing on me!

    Hey all, I've been working on this project for awhile and after stepping through the debugger, I noticed why it wasn't working. An integer is changing its value by itself.. Due to the complexity of the code, here's an example:

    Code:
    typedef struct Structure
    {
        int x;
        int y;
    }Structure;
    
    class Test
    {
    protected:
        int Count;
        Structure* m_Struct;
    public:
        void SetStructure(int MaxCount);
    };
    
    // Count is set to 0 in the Constructor
    // m_Struct is allocated correctly.
    
    void Test::SetStructure()
    {
        // Count is 0!!!
    
        m_Struct[Count].x = 24;  // COUNT IS NOW 45???
        m_Struct[Count].y = 54;  //  COUNT IS 45 AGAIN
    
        Count++;
    }
    Count is setting itself to 45 when I use it as an index. Why is this happening??
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  2. #2
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,812
    Well does it happen in that example because it doesn't happen to me.
    Code:
    #include <iostream>
    
    struct Structure
    {
        int x;
        int y;
    };
    
    class Test
    {
        public:
            Test()
            {
                count = 0;
                testStruct = new Structure[20];
            }
            void SetStructure(int Maxcount = 20)
            {
                testStruct[count].x = 10;
                testStruct[count].y = 20;
                std::cout<<"Count = "<<count<<std::endl;
                count++;
            }
            ~Test()
            {
                delete [] testStruct;
            }
        protected:
            int count;
            Structure *testStruct;
    };
    
    int main()
    {
        Test mainTest;
        for(int i = 0; i < 10; i++)
        {
            mainTest.SetStructure();
        }
        
        std::cin.get();
        
        return 0;
    }
    Are you sure you are not using the value of maxCount as your index and not count?
    Woop?

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    ah, I just noticed what's happening. I'm using a RECT as an argument and its replacing Count with rect.bottom. Crazy crazy stuff. I just made a little console app to test it and it works fine with that, so I'm assuming I'm out of memory or something. There might be something wrong with my code also, take a look:

    Class structure
    Code:
    class __declspec(dllexport) ArkButton
    {
    protected:
    	LPDIRECT3DTEXTURE9 m_Texture;
    	LPD3DXSPRITE m_Sprite;
    	sButton *m_Button;
    	int ButtonCount;  // <<<<<<<<<<<<<<<
    Function in question:
    Code:
    BOOL ArkButton::CreateButton(RECT *pSrcRect, void (*Function)(void* DataPtr), float XPosition, float YPosition, float ZPosition, RECT *pOverRect, RECT *pDownRect )
    {
                   // BUTTONCOUNT is 0!!!
    
    	if( pSrcRect == NULL || Function == NULL )
    		return FALSE;
    
    
    	m_Button[ButtonCount].m_XPos = XPosition; //BUTTONCOUNT IS pSrcRect.bottom!!!!
    	m_Button[ButtonCount].m_YPos = YPosition; //SAME ALL THE WAY DOWN
    	m_Button[ButtonCount].m_ZPos = ZPosition;
    	m_Button[ButtonCount].srcRect = pSrcRect;
    	m_Button[ButtonCount].Function = Function;
    
    	if( pOverRect != NULL )
    		m_Button[ButtonCount].overRect = pOverRect;
    	if( pDownRect != NULL )
    		m_Button[ButtonCount].downRect = pDownRect;
    
    	ButtonCount++;
    	return TRUE;
    }
    and the calling code:
    Code:
    AB->Init(AC, "C:\\UI\\Buttons.jpg", 2);
    	RECT rect;
    	rect.left = 0;
    	rect.top = 0;
    	rect.right = 40;
    	rect.bottom = 45;  // THIS WILL BE BUTTONCOUNT =\
    	AB->CreateButton(&rect, &tempfunction, 200, 200, 0);
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    One place to look for these kinds of errors is array buffer overruns. Check to make sure you never access memory past array bounds. For example, if you have an array of 10 ArkButton objects, only indexes 0-9 are valid.

    Step through the debugger if you can and look at the memory locations of the ArkButton instance that has the problem and see if it changes on that line.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    If ButtonCount is changing from 0 to some other value just after calling the following code:
    Code:
    if( pSrcRect == NULL || Function == NULL )
    		return FALSE;
    Then the problem isn't due to a buffer overrun/underrun. The only thing that could be causing your problem that I can think of is if you have another thread running which is changing data in memory. Do what Daved suggested, and watch the memory location that ButtonCount represents, and see exactly which line of code is causing it to change.

  6. #6
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Is there a way to watch stuff in structures? I try but fail to get: Error: Expression cannot be evaluated. Any way around this?
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  7. #7
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,265
    Yeah, just watch the memory address. If you are using Visual C++, I think you can just press Alt+6 while debugging to open up a memory window.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,845
    > AB->CreateButton(&rect, &tempfunction, 200, 200, 0);
    ...
    > m_Button[ButtonCount].srcRect = pSrcRect;

    Here's one problem.
    Your class member variable is merely storing a pointer to something which is outside the class. If the external local variable goes out of scope you end up pointing to random bits of stack space being used by other local variables.

    Classes should only have their own data or data which they themselves allocate.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #9
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    Quote Originally Posted by Salem
    > AB->CreateButton(&rect, &tempfunction, 200, 200, 0);
    ...
    > m_Button[ButtonCount].srcRect = pSrcRect;

    Here's one problem.
    Your class member variable is merely storing a pointer to something which is outside the class. If the external local variable goes out of scope you end up pointing to random bits of stack space being used by other local variables.

    Classes should only have their own data or data which they themselves allocate.
    NICE EYES..dang! I had that problem and was debugging it for hours, I should have checked the damn forum! But yea, I finally saw it and fixed it. Again, very nice spot.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. tools for finding memory leaks
    By stanlvw in forum C++ Programming
    Replies: 4
    Last Post: 04-03-2009, 12:41 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Suggestions on this C style code
    By Joelito in forum C Programming
    Replies: 11
    Last Post: 06-07-2007, 04:22 AM
  4. Program that displays amount of free memory
    By trancedeejay in forum Linux Programming
    Replies: 3
    Last Post: 01-13-2006, 01:27 PM
  5. Shared Memory - shmget questions
    By hendler in forum C Programming
    Replies: 1
    Last Post: 11-29-2005, 02:15 AM

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