openGL app not working outside of visual studio.

Started by
4 comments, last by Nercury 11 years, 1 month ago

I'm not really sure how to go about saying this so i'll just try to explain and post pictures when ever needed.

http://puu.sh/2eQz7

as you can see, running this in visiual studio works........but when tried to run like this........

http://puu.sh/2eQv2

Im not sure what it could be......

Advertisement

You have a broken pointer somewhere in your code. I think that one in particular is related to freeing a pointer; you're trying to free a pointer that doesn't have a valid value, for example double deleting a pointer, or deleting an uninitialized pointer.

I don't know if it would help, but try to build as Release not Debug.

It could really be anything.

It can be


Thing *t = new Thing();
delete t;
delete t; // cleaning same memory twice

Or


char *c = new char[10];
c[10] = 10; // array writing out of bounds, you can overwrite some other heap object without even a warning!
// I think this is worst kind of thing, because your error will happen somewhere else, and will be unpredictable

Or


Thing * returnIt() {
    Thing t;
    return &t; // if you return pointer to local variable it no loger points to valid memory
}

Thing *t = returnIt();
t->use(); // crash

Many things. Actually here is a list from stack overflow:

Common scenarios include:

  • Writing outside the allocated space of an array (char *stuff = new char[10]; stuff[10] = 3;)
  • Casting to the wrong type
  • Uninitialized pointers
  • Typo error for -> and .
  • Typo error when using * and & (or multiple of either)
  • Mixing new [] and new with delete [] and delete
  • Missing or incorrect copy-constructors
  • Pointer pointing to garbage
  • Calling delete multiple times on the same data
  • Polymorphic baseclasses without virtual destructors

Ok, thanks, i figured it out, now......there's another problem.... for example i did something like this:

http://puu.sh/2f7Gu

as u can see, added vertices all around the circles (need to do some management down the line and if look at the console, you can see that both points are the same but theyt shouldn't be what i did was basically this:


	void draw() {
		glBegin(GL_POLYGON);

		
		for(mAngle = 0.0; mAngle <= 2 * 3.14; mAngle += (3.14/this->mNumSegments))
		{
			
			glVertex2d(mXpos + mRadius * -cos(mAngle),
				       mYpos + mRadius * sin(mAngle));
			
			glColor3d(mRed,mGreen,mBlue);
			
		}
		glColor3d(mRed,1,1);
		
		glEnd();
		
			for(mAngle = 0.0; mAngle <= 2 * 3.14; mAngle += (3.14/mNumSegments))
			{
				for(int i = 0; i < mNumSegments; i++)
				{
				
					points.reserve(i);
					points.resize(i);
					points.push_back(new Points);
				
					glColor3d(1,0,0);
					for(vector<Points*>::iterator i = points.begin(); i != points.end(); i++)
					{
						Points* p = (*i);
						if(p != NULL) {
							p->setPosition(mXpos + mRadius * cos(mAngle),
									  mYpos + mRadius * sin(mAngle));
							glPointSize(5);
							p->draw();
						}
						else
						{
							delete p;
							p = 0;
						}
					}
				}
				
				
			}
	

as u can see, it adds 1 point to each segment of the circle. I added this to an array vector so i know it works. As you can see, all the points render to their respective segments on the circle. My problem is getting the points and them being different.....as i said, I got two points that I thought would be with different values since all the points got rendered to the circle, I figured it would be ok........but it's not.


Points* getPoint(int index) 
	{
		vector<Points*>::iterator i = points.begin();

		for(i; i != points.end();i++)
		{
			return points[index];
		}
	}

this gets the points based on index (ignore that for loop doesn't really do anything.....so you would expect p[0] and p[1] to have different values? but they don't. this is the same problem i've been having in java with a programming assignment in class.

here's what im trying to do (you might of guessed from the screen shot.)


	cout << "Num of points = " << faces[0]->getNumPoints() << endl;
	Points* p1 = faces[0]->getPoint(0);
	cout << "p0 pos = " << p1->getX() << " " << p1->getY() << endl;
	glBegin(GL_LINES);
		glVertex2d(p1->getX(),p1->getY());
		glVertex2d(p1->getX()+40,p1->getY()+10);
	glEnd();
	Points* p2 = faces[0]->getPoint(4);
	cout << "p1 pos = " << p2->getX() << " " << p2->getY() << endl;
	glBegin(GL_LINES);
		glVertex2d(p2->getX(),p2->getY());
		glVertex2d(p2->getX()+10,p2->getY()+10);
	glEnd();
	

Ok, I am just going to glance over your code...


for(mAngle = 0.0; mAngle <= 2 * 3.14; mAngle += (3.14/this->mNumSegments))
{
    //
}

Never use floating-point values for iteration, because floating point errors accumulate and can cause problems sooner than you think. Use integers:


double segmentSize = 3.14/this->mNumSegments;
for (int segment = 0; segment <= this->mNumSegments; segment++) {
     mAngle = segment * segmentSize;
}

You would make your life easier if you avoid modifying or creating your data on draw. Set up everything beforehand, and just read what to draw.

I especially suggest to rethink this one:


points.reserve(i);
points.resize(i);
points.push_back(new Points);

glColor3d(1,0,0);
for(vector<Points*>::iterator i = points.begin(); i != points.end(); i++)
{
	Points* p = (*i);
	if(p != NULL) {
		p->setPosition(mXpos + mRadius * cos(mAngle),
				  mYpos + mRadius * sin(mAngle));
		glPointSize(5);
		p->draw();
	}
	else
	{
		delete p;
		p = 0;
	}
}

It looks like it would be extremely hard to manage point lifecycle when they are created and deleted in this way. Of course, I do notice that you try to attempt to delete a null pointer sometimes:


Points* p = (*i);
if(p != NULL) {
	// ...
}
else
{
	delete p; // p is NULL!
	p = 0;
}

And.. it looks like for every new triangle point, you are iterating and drawing all the points.

Ok, so my advice is to create/delete objects away from drawing, use update step for that. Only read data in draw.

This topic is closed to new replies.

Advertisement