Click here to Skip to main content
15,896,606 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey,

I got the following problem.

I got 2 methods, each part of a different class

C#
GPA Forge()
	{
	GPA Result=GPA(xtotal,ytotal);
	for (int n=0;n<=X;n++){
	for (int m=0;m<=Y;m++){
	GPA Source =(*this)[n][m];
	Result.Insert(Source,0,0,n*xrange,m*yrange,xrange,yrange);
	}
	}
	return Result;
	}


and

C#
//This function is part of the GPA class
void Insert (GPA Source,int xSource, int ySource,int xHere, int yHere, int xArea,int yArea)
{
for(int m=0;m<yArea;m++){
for(int n=0;n<xArea;n++){
(*this)[xHere+n][yHere+m]=Source[xSource+n][ySource+m];
}
}
}


both functions work in the current form. However, after Insert(...) is complete for the first run, the GPA destructor

C#
~GPA()
{
        delete [] Addr;
}


is run twice instead of once due to some reason. The second run, causes a "BLOCK ASSERT FAILED..." and crashes the application.

I'd appreciate ideas on how to fix this.
Thanks in advance
Posted
Updated 11-Apr-12 8:43am
v3

1 solution

Without knowing what GPA actually is, i would say, when it's assignment operator is used (eg: gpa1 = gpa1) it will copy the pointer over to the other instance, not create a copy of whatever it points to. So you will have multiple GPA instances pointing at the same allocated memory and when it comes to destructing these GPA instances, the first destructor deletes the memory, the second one also tries to delete the very same memory block, but since it has already been deleted by the first one, BANG.

Ah, and it runs twice because in your Forge method you have 2 instances of GPA created on the stack (Result and Source) which are destructed when the function returns.
 
Share this answer
 
v4
Comments
Y0UR 11-Apr-12 15:24pm    
That helped, thanks :)
Code-o-mat 11-Apr-12 16:30pm    
Yourwelcome :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900