Advertisement

Problem with this code?

Started by August 07, 2000 11:26 PM
28 comments, last by Esap1 24 years, 4 months ago
Brase yourself, its kinda of long But Im having a problem with this code. I get an Access Violation on the First line of the Octree Destructor. Heres the code.
    


class COctree 
{

protected:
	//Member Variables

	COctree			*child[8];
	unsigned int	numVertex;
	CVertex			*Vertex;
	CBox			*box;
	bool			isroot;
	bool			isOK;

public:
	//Member Functions

	void			Init(){isOK=true;}
	void			End(){isOK=false;}
	bool			IsOK(){return isOK;}

	bool			isLeaf();
	bool			build(CVertex *Points, 
							const unsigned int count, 
							const unsigned int threshold,
							const unsigned int maximumDepth,  
							const unsigned int currentDepth = 0);
	void			Draw();
	unsigned int	getnumVertex(){return numVertex;}
	COctree*		getOctant(unsigned short id){return child[id];}
	CBox*			getBox(){return box;}

	int				traverse();
	
	//Constructor/Destructor

	COctree()
	{
		child[0]=0;
		child[1]=0;
		child[2]=0;
		child[3]=0;
		child[4]=0;
		child[5]=0;
		child[6]=0;
		child[7]=0;
		Vertex=0;
		box=0;
	}
	~COctree()
	{
		isOK=false;
		if(child[0])delete child[0];
		if(child[1])delete child[1];
		if(child[2])delete child[2];
		if(child[3])delete child[3];
		if(child[4])delete child[4];
		if(child[5])delete child[5];
		if(child[6])delete child[6];
		if(child[7])delete child[7];
		if(Vertex)delete [] Vertex;
		if(box)delete box;
	}

	
};




bool	COctree::build(CVertex *Points, const unsigned int count, const unsigned int threshold,
			const unsigned int maximumDepth,  
			const unsigned int currentDepth)
{
	unsigned int i;
	unsigned int NewCount=0;
	unsigned int TempCount=0;
	float cxmin = 0.0, cxmax = 0.0;    
	float cymin = 0.0, cymax = 0.0;
    float czmin = 0.0, czmax = 0.0;
	float bigest=0;
	
	
	//if root 

	if(currentDepth==0)
	{
		for(i = 0; i < count; i++) 
		{                
			//FIX THIS!!!!!!!

			
			// Minimums.

			if(Points<i>.getx() < cxmin)
	            cxmin = Points[i].getx();        
       
			if(Points[i].gety() < cymin)
	            cymin = Points[i].gety();        
        
			if(Points[i].getz() < czmin)
	            czmin = Points[i].getz();        
        
			// Maximums.

			if(Points[i].getx() > cxmax)
	            cxmax = Points[i].getx();        
        
			if(Points[i].gety() > cymax)
			    cymax = Points[i].gety();        
        
			if(Points[i].getz() > czmax)
			    czmax = Points[i].getz();    
		}
		
		if(-cxmin>bigest)bigest= -cxmin;
		if(-cymin>bigest)bigest= -cymin;
		if(-czmin>bigest)bigest= -czmin;
		if(cxmax>bigest)bigest=cxmax;
		if(cymax>bigest)bigest=cymax;
		if(czmax>bigest)bigest=czmax;
		
		cxmin=cymin=czmin=-bigest;
		cxmax=cymax=czmax=bigest;
		
		box = new CBox(cxmin,cxmax,	cymin,cymax, czmin,czmax);
	}

	
	for(i=0; i<count; i++)
	{
		if(box->CheckVertex(Points[i])==true)TempCount++;
	}
	 
	
	CVertex *NewPoints = new CVertex[TempCount];

	
	//Check if it is a Leaf

	if(TempCount<=threshold || currentDepth>= maximumDepth)
	{
		//WE HAVE A LEAF

		numVertex = TempCount;
		Vertex = new CVertex [count];
		memcpy(Vertex, Points, sizeof(CVertex) * TempCount);
		return true;        
	}



	for(i = 0; i < count; i++)
	{
	
		if(NewCount > threshold || currentDepth < maximumDepth)
		{
			if(box->CheckVertex(Points[i])==true)
			{
				NewPoints[NewCount] = Points[i];
				NewCount++;
			}
			
		}
		
	
	}
	
	numVertex = NewCount;	

	for(i = 0; i < 8; i++)
	{            
		child[i] = new COctree;
	}
	
	
	

	
	
	
	
	// Octant 0.

	child[0]->box = new CBox(
		box->xmin,
		box->xmin + ((box->xmax - box->xmin) * 0.5f),
		box->ymin,
		box->ymin + ((box->ymax - box->ymin) * 0.5f),
		box->zmin,
		box->zmin + ((box->zmax - box->zmin) * 0.5f));
	
	// Octant 1.

	child[1]->box = new CBox(
		box->xmin,
		box->xmin + ((box->xmax - box->xmin) * 0.5f),
		box->ymin,
		box->ymin + ((box->ymax - box->ymin) * 0.5f),
		box->zmin + ((box->zmax - box->zmin) * 0.5f),
		box->zmax);
	
	// Octant 2.

	child[2]->box = new CBox(
		box->xmin,
		box->xmin + ((box->xmax - box->xmin) * 0.5f),
		box->ymin + ((box->ymax - box->ymin) * 0.5f),
		box->ymax,
		box->zmin,
		box->zmin + ((box->zmax - box->zmin) * 0.5f));
	
	// Octant 3.

	child[3]->box = new CBox(
	box->xmin,
		box->xmin + ((box->xmax - box->xmin) * 0.5f),
		box->ymin + ((box->ymax - box->ymin) * 0.5f),
		box->ymax,
		box->zmin + ((box->zmax - box->zmin) * 0.5f),
		box->zmax);
	
	// Octant 4.

	child[4]->box = new CBox(
		box->xmin + ((box->xmax - box->xmin) * 0.5f),
		box->xmax,
		box->ymin,
		box->ymin + ((box->ymax - box->ymin) * 0.5f),
		box->zmin,
		box->zmin + ((box->zmax - box->zmin) * 0.5f));
	
	// Octant 5.

	child[5]->box = new CBox(
		box->xmin + ((box->xmax - box->xmin) * 0.5f),
		box->xmax,
		box->ymin,
		box->ymin + ((box->ymax - box->ymin) * 0.5f),
		box->zmin + ((box->zmax - box->zmin) * 0.5f),
		box->zmax);
	
	// Octant 6.

	child[6]->box = new CBox(
		box->xmin + ((box->xmax - box->xmin) * 0.5f),
		box->xmax,
		box->ymin + ((box->ymax - box->ymin) * 0.5f),
		box->ymax,
		box->zmin,
		box->zmin + ((box->zmax - box->zmin) * 0.5f));
	
	// Octant 7.

	child[7]->box = new CBox(
		box->xmin + ((box->xmax - box->xmin) * 0.5f),
		box->xmax,
		box->ymin + ((box->ymax - box->ymin) * 0.5f),
		box->ymax,
		box->zmin + ((box->zmax - box->zmin) * 0.5f),
		box->zmax);
	

	for(i = 0; i < 8; i++)
		child[i]->build(NewPoints, NewCount, threshold, maximumDepth, currentDepth+1);
		

	
	return true;
}

    
Theres the code. Any problems in it? PS: Sorry for the Length.
ummm do you mean the Isok=false line?
or the first delete?

Great Milenko

Words Of Wisdom:
"Never Stick A Pretzel In Your Butt It Might Break Off In There."


http://www.crosswinds.net/~milenko
http://www.crosswinds.net/~pirotech

The Great Milenko"Don't stick a pretzel up your ass, it might get stuck in there.""Computer Programming is findding the right wrench to hammer in the correct screw."
Advertisement
Hi..

Try to insert your member fumction End(); instead of your iOK=false; --- does it work?



Denis "Mr.Snow" Kozhukhov
CEO & Lead programmer
Choco Snow Creation
=============================Denis "Mr.Snow" KozhukhovCEO & Lead programmerChoco Snow CreationdkcscPortal=============================
The isOK=false line gives me an Access Violation, and If I delete that line, then the first line(which is the first delete) also gives me an Access Violation.

MrSnow: Now the Access Vilation is in End()(isOK=false, in the End() function).

I JUST GOT NO IDEA WHATS HAPPENING!!!!!!!
Anyone?

Back to the top.
Looks to me like you might be deleting something that hasn''t been allocated, like an uninitialized pointer? Try this:

1) initialize all pointers to NULL in the constructor (you''ve done this, good going)
2) before you allocate anything, make sure it''s NULL beforehand.
3) When you delete something, set the pointer back to NULL.

You can consider these "debug" steps and remove them once you''ve found your bug, but they''re all pretty good practices to have. Anyway, this should help you with your debugging. Otherwise, just step through all the functions.

Also, I noticed you didn''t declare a destructor in your class definition. Mistype?
Advertisement
Comment out all of the code in the destructor. If you're still getting access violations, then it's a problem in the use of the COctree object. Unfortunately those kinds of bugs can be the most difficult and annoying to fix, often requiring you to rethink and/or rewrite your entire app framework. Anyway.. you do know how to use a debugger, right?

I realized that message was kind of vague, so.. to put it in context, I was recently getting very similar problems. An int member of one of my classes was giving me access violations whenever I tried to access it. Turns out that particular object (which was part of an array) had never been allocated in the first place, it was residing in the small space of extra memory that new returns. The one variable causing the access fault was the only one that didn't fit in that extra space of bunk memory. The moral of the story? Make sure you aren't trying to do something like this:

    object myArray[1][1][1];myArray[0][1][0].func();    


Because sometimes it might actually work and that can cause really deep bugs.

-RWarden (roberte@maui.net)

Edited by - RWarden on August 8, 2000 7:27:58 PM
I commented the Destructor out and theres no Access Violation.
What I dont understand is why the Destructor of a non-existant COctree is being called?
Also, I no little to nothing about debugging in VC6++. All I know how to do is to set brake points. But that wont help much since this is a recursive function, and is happening the the program tries to end.
And again to the top
Something is going out of scope that hasnt been properly allocated. Do like someone posted above -

1) Allocate all of your pointers to NULL.
2) EVERY time you allocate memory check to see if the allocation succeeded afterwards.

if( (blah = new cBlah) == NULL)
uh oh!!

skitzo_smurf
"Innocent is just a nice way to say ignorant, and stupidity is ignorance with its clothes off."words of,skitzo_smurf

This topic is closed to new replies.

Advertisement