Assertion error.

closed account (Sy0XoG1T)
Using this code gives me a "Debug assertion failed" when I close out of the program.

Drawable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
namespace ec
{
	////////////////////////////////////////////////////////////
	/// Base abstract class for drawable classes.
	///
	////////////////////////////////////////////////////////////
	class Drawable
	{
	protected:
		////////////////////////////////////////////////////////////
		/// SFML base abstract class.
		///
		////////////////////////////////////////////////////////////
		sf::Drawable * Base;

		////////////////////////////////////////////////////////////
		/// Dimensions.
		///
		////////////////////////////////////////////////////////////
		float X, Y;
		float TOP, BOTTOM, LEFT, RIGHT;
		unsigned int WIDTH, HEIGHT;

	public:
		////////////////////////////////////////////////////////////
		/// Default constructor and destructor.
		///
		////////////////////////////////////////////////////////////
		Drawable();
		~Drawable();
	};

	class Rect : public Drawable
	{
	private:
		sf::Shape Shape;
	public:
		////////////////////////////////////////////////////////////
		/// Constructor.
		///
		////////////////////////////////////////////////////////////
		Rect(float X, float Y, float X2, float Y2, sf::Color Fill, sf::Color Outline);
	};
}


Rect.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
ec::Rect::Rect(float X, float Y, float X2, float Y2, sf::Color Fill, sf::Color Outline)
{	
	
	Shape = sf::Shape::Rectangle(X, Y, X2, Y2, Fill, 1, Outline);
	Base = &Shape;

	LEFT	= X;
	RIGHT	= X2;
	TOP	= Y;
	BOTTOM	= Y2;
	WIDTH	= ((unsigned int)RIGHT - LEFT);
	HEIGHT	= ((unsigned int)BOTTOM - TOP);
	this->X = LEFT + (WIDTH / 2);
	this->Y = TOP + (HEIGHT / 2);
}


Drawable.cpp
1
2
3
4
5
ec::Drawable::Drawable(){}
ec::Drawable::~Drawable()
{
	delete Base;
}


But If I get rid of the delete Base; in Drawable's deconstructor I don't get the error anymore.

1. I'm curious as to why that is.
2. If I remove delete Base; will this cause memory leaks?
ec::Rect::Base isn't being allocated dynamically, so it doesn't need to be deleted. Furthermore, it should not be deleted. Each instance of the new keyword should be matched with an instance of the delete keyword (this sentence isn't strictly true, but it's a good rule of thumb). If you didn't use new, don't use delete.
closed account (Sy0XoG1T)
Ahh I see. I thought all pointers just needed to be deleted or they'll be left over memory until the program closes. Thanks.
You still have undefined behavior in the program. The constructor is assigning a pointer to a locally constructed object to the pointer. Helios is correct but that isn't the only problem in your program.
The constructor is assigning a pointer to a locally constructed object to the pointer.
No, it isn't. Shape is a member.
1
2
Shape = sf::Shape::Rectangle(X, Y, X2, Y2, Fill, 1, Outline);
	Base = &Shape;


Helios, where is the sf::shape type defined? Do you see it? What is the Rectangle? Is that a static function or a constructor? Without that information I do not see how to determine what is really happening in that line of code. It looked like it was calling a constructor or static function that returns an object or a pointer to an object.

Sigh...

where is the sf::shape type defined?
Right here: http://www.sfml-dev.org/documentation/1.6/classsf_1_1Shape.htm

What is the Rectangle?
That would be this: http://www.sfml-dev.org/documentation/1.6/classsf_1_1Shape.htm#aec9c0468a77871f6410aea757105498

Without that information I do not see how to determine what is really happening in that line of code. It looked like it was calling a constructor or static function that returns an object or a pointer to an object.
That doesn't change the fact that Shape is a member and that Base will remain valid for the entire lifetime of the ec::Rect object. There's not a single type you can give to Shape that will make the program compile but incorrect.
Topic archived. No new replies allowed.