-
May 22nd, 2008, 03:42 AM
#1
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 .
-
May 22nd, 2008, 03:49 AM
#2
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
-
May 22nd, 2008, 04:04 AM
#3
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
-
May 22nd, 2008, 04:20 AM
#4
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.
-
May 22nd, 2008, 04:36 AM
#5
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;
-
May 22nd, 2008, 04:41 AM
#6
Re: Runtime debug error.
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
-
May 22nd, 2008, 04:43 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|