Hi,

I'm working on a project for one of my classes and I was trying to use the std::vector class to store pointers to a base class DualObject that will be used to store different types of objects like CircleDual, LineDual, etc. which all inherit from DualObject. Most of them work fine, but whenever I try to add the CircleDual object to this class it sends a bad_alloc. Seeing that they are all the same base class, this shouldn't happen. I then tried it again with the std::list class but ran into the same problem. Below are the portions of the code that seem relevant to me.

Portion of code from my mouse event handler. The addition of a PointDual * works fine, but the CircleDual * does not:

Code:
vector<DualObject *> *objectList = NULL;
objectList = new std::vector<DualObject *>();
Code:
	/* Create a point */
if ( oldMouseX == x && oldMouseY == y )
{
	PointDual *newPD;
	try
	{
		newPD = new PointDual(translateX(x), translateY(y), 20.0, 20.0);
		newPD->setColor(0.5, 1.0, 0.0);

		/* Add it to the list. */
		objectList->push_back(newPD);
	}
	catch (exception &e)
	{
		cerr << e.what() << " at line " << __LINE__ << endl;
	}
	catch(...)
	{
		cerr << "Error occurred, not sure what it was. (Line: " 
			<< __LINE__ << ")\n" << endl;
	}
	
}
else if ( button == GLUT_LEFT_BUTTON 
			&& keyModifiers == GLUT_ACTIVE_SHIFT ) /* Draw a circle. */
{
	CircleDual *newCircle;

	try
	{
		newCircle = new CircleDual(translateX(oldMouseX), translateY(oldMouseY),
			sqrt(pow(translateX(x) - translateX(oldMouseX), 2.0) + 
			pow(translateY(y) - translateY(oldMouseY), 2.0)), 20.0, 20.0);

		/* Add it to the list. */
		objectList->push_back((DualObject *)newCircle);
	}
	catch (exception &e)
	{
		cerr << e.what() << " at line " << __LINE__ << endl;

	}
	catch(...)
	{
		cerr << "Error occurred, not sure what it was. (Line: " 
			<< __LINE__ << ")\n" << endl;
	}
}
Here are the PointDual and CircleDual Definitions:

Code:
class PointDual: public DualObject {

private:
	double _x, _y;
    double x0, y0, x1, y1;

public:
    /**
     * Default constructor. This creates a PointDual that has a point at
     * location (x, y) in real world coordinates.
     *
     * @params x The x coordinate of the point.
     * @params y The y coordinate of the point.
     * @params width The width of the window.
     * @params height The height of the window.
     */
    PointDual(double x, double y, double width, double height)
    {
	    x0 = -width / 2.0;
	    x1 = width / 2.0;
	    y0 = x * x0 + y;
	    y1 = x * x1 + y;

	    _x = x;
	    _y = y;

	}
    
    /**
     * Draw the object normally. This draws the object as described in
     * the regular coordinate system. The base class (this) does nothing.
     */
    virtual void draw()
    {
		glColor3fv(color);
		glBegin(GL_POINTS);
			glVertex2f(_x, _y);
		glEnd();
    }
    
    /**
     * Draw the object's dual. This draws the object's dual as described
     * by the given extended class. The base class (this) does nothing.
     */
    virtual void drawDual()
    {
		glColor3fv(color);
		glBegin(GL_LINES);
			glVertex2f(x0, y0);
			glVertex2f(x1, y1);
		glEnd();
    }
};

class CircleDual : public DualObject
{
private:
	double centerX, centerY;
	double radius;
	double width, height;

	static const int numberOfSegments = 32;
	double *segmentList;

    void drawPointDual(double x, double y)
    {
	    double x0 = -width / 2.0;
	    double x1 = width / 2.0;
	    double y0 = (double)x * x0 + (double)y;
	    double y1 = (double)x * x1 + (double)y;

		glColor3fv(color);
		glBegin(GL_LINES);
			glVertex2f(x0, y0);
			glVertex2f(x1, y1);
		glEnd();
    }

public:

	CircleDual()
	{
		centerX = centerY = radius = 0.0;

		/* Don't create a segment list. */
		segmentList = NULL;
	}

	CircleDual(double x, double y, double r, double width, double height)
	{
		/* Copy the circle parameters. */
		centerX = x;
		centerY = y;
		radius = r;
		segmentList = NULL;

		this->width = width;
		this->height = height;

		segmentList = new double[numberOfSegments];

		for (int i = 0; i < numberOfSegments; i += 2)
		{
			double x0, y0;

			x0 = radius * cos(i * 2.0 * PI / numberOfSegments);
			y0 = radius * sin(i * 2.0 * PI / numberOfSegments);

			segmentList[2*i] = x0 + centerX;
			segmentList[2*i + 1] = y0 + centerY;
		}
	}

	~CircleDual()
	{
		if ( segmentList )
			delete [] segmentList;
	}

	/**
	 * Draw the circle normally using a sequence of line segments.
	 */
	virtual void draw()
	{
		glBegin(GL_LINE_LOOP);
		for (int i = 0; i < numberOfSegments; i += 2)
		{
			glVertex2d(segmentList[2*i], segmentList[2*i + 1]);
		}
		glEnd();
	}

	/**
	 * Draw the dual of the circle. For now, just draw the dual of the line segments.
	 */
	virtual void drawDual()
	{
		for (int i = 0; i < numberOfSegments; i += 2)
			drawPointDual(segmentList[2*i], segmentList[2*i + 1]);
	}

};
Another interesting thing to note is once I catch the bad_alloc, any other actions on std::vector<DualObject *>objectList throws a bad alloc as well. I'm not very familiar with the STL so perhaps I'm forgetting to do something before exiting the exception handler.

If anyone has any idea of what I may be doing wrong it would be greatly appreciated.