CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    May 2008
    Posts
    2

    Unhappy Runtime debug error.

    Hi, since 2 days I have been stuck on a test program I am making. When I try to run my program in the debugger it gives the following error:

    Unhandled exception at 0x00708a0a in test.exe: 0xC0000005: Access violation reading location 0xfeeefefa.

    Here is a little peice of the code I am using and where it seem to happen.

    I use a structure with a destructor:
    Code:
    struct ExpDat
    {
    ~ExpDat()
    	{		
    		for (int i = 0;i < 10;i++)
    		{
    			if(Children[i] != NULL)
    			{
    				delete Children[i]; 
    			}		
    		}			
    	}
    ExpDat *Childeren[10];
    };
    When I get the error in the debugger, it points to the line that says:
    "if(Children[i] != NULL)" And gives the error I already said.

    Here is another peice of code that is involved with it:

    Code:
    int c = 0;
    ExpDat *ParseExpression1(ExpDat *data)
    {
    ExpDat *tmp = data;	
    ExpDat *dat = new ExpDat;
    int res = 0;
    
    
    	if(c < 10)
    	{
    		dat->value1 = res;		
                    tmp->Childeren[c] = dat;		
    		c++;
    	}	
    
    	delete dat;
    	return tmp;
    }
    Now, when I comment-out the rule "tmp->Childeren[c] = dat;", it doesn't seem to give the error. Also, when I remove the 'delete' call, It runs fine as well.

    This is not the complete code, some code is missing here, if you need the other part, I can post it.


    I am not that a good coder so I can't figure out how to fix this myself, but it's probably something stupid and easy to fix.

    Can someone with a good C++ knowledge please help me out .

  2. #2
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Runtime debug error.

    My guess is that some of the pointers in the array are not initialized. When creating the array set all the elements to NULL and see if the error still occurs.
    Har Har

  3. #3
    Join Date
    Apr 2008
    Posts
    133

    Re: Runtime debug error.

    Also you seem to do:
    tmp->Childeren[c] = dat;

    then in the following line you 'delete dat' which means tmp->Childeren[c] is deleted so if you try to delete it again in d'tor you will have a problem

  4. #4
    Join Date
    May 2008
    Posts
    2

    Re: Runtime debug error.

    Thanks for the fast reply guys . I see what the problem is now. So the 'delete dat;' call, is actually useless, and can be removed?

    Also, I did initialize the pointer, it happens in the constructor, I didn't post it though.

  5. #5
    Join Date
    Apr 2008
    Posts
    133

    Re: Runtime debug error.

    since you are storing the pointer its better not to delete in that case..
    you could move the delete to 'else' like

    if(c < 10)
    {
    dat->value1 = res;
    tmp->Childeren[c] = dat;
    c++;
    }
    else
    delete dat;

  6. #6
    Join Date
    Aug 2002
    Location
    Cluj-Napoca,Romania
    Posts
    3,496

    Re: Runtime debug error.

    Quote Originally Posted by Thomas_324
    Thanks for the fast reply guys . I see what the problem is now. So the 'delete dat;' call, is actually useless, and can be removed?

    Also, I did initialize the pointer, it happens in the constructor, I didn't post it though.
    If you plan to use that value further than that delete call is not useless it's a bug.
    Har Har

  7. #7
    Join Date
    Aug 2007
    Location
    Birmingham, UK
    Posts
    360

    Re: Runtime debug error.

    I may be missing an important point, but why are you using new and delete to create (what looks like to me) a local object in ParseExpression? It looks a bit Java-ish to me. In C++ you can just create the object on the stack and let the compiler take care of construction and destruction.

    I do see that you assigned the pointer to dat to something else, but that pointer will be invalid as soon as you've called delete on it, regardless of the number of copies you hold of that pointer.

    In any case - it is always a good habit to assign "NULL" to pointers after calling delete on them to prevent deleting the same pointer again in the dtor.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured