[SDL] Weird exception

Started by
10 comments, last by Gage64 15 years, 7 months ago
Quote:Original post by Gage64
Quote:Original post by Mybowlcut
XImage's default constructor is just empty. It calls Surface's default constructor because it's the default constructor - I don't get why this is an issue.


It's not necessarily an issue. It's just that when you have a constructor that does nothing, you are enabling the creation of the object with an undefined state (i.e., the object's member variables contain garbage values).

Often people just stick an empty default constructor for their classes out of convenience. I just wanted you to think if you really need it.

Anyway, you say that after you call read_data(), the vector of images contains garbage values. Did you step through that function with the debugger? (this is actually the first thing I should have asked)

EDIT:

Quote:read_data just returns a container of objects as specified from the file... The problem is there. Once the image is constructed and read_data finishes, I look at the contents of v and it has the image but the internals of the surface are all messed up


Actually it sounds like you did this already.

One other thing - does XImage has a proper copy constructor?
Cheers for replying this far. :)

I remember reading somewhere that using the operator= function as a lazy replacement for a copy constructor is bad practice... is that right?
XImage::XImage(const XImage& rhs){	*this = rhs;}XImage& XImage::operator=(const XImage& rhs){	if(this == &rhs) return *this;	this->name = rhs.name;	this->file_name = rhs.file_name;	this->surface = rhs.surface;	this->position = rhs.position;	this->is_colour_key = rhs.is_colour_key;	this->colour_key = rhs.colour_key;	return *this;}
I debugged it again... and it seems that the surface is invalidated after the call to std::copy in read_data:
template <typename Object, typename Container>	Container read_data(std::istream& file)	{		Container v;		//if(get_file_size(file) == 0)		//{ // Empty; return early.		//	return v;		//}		std::copy(			std::istream_iterator<Object>(file),			std::istream_iterator<Object>(),			std::back_inserter(v));		return v; // here	}


Ah... I tried some more testing:
Surface s1("3.png");	{		Surface s2("3.png");	}	return 0;
s1's surface screws up after that block executes... so I have issues somewhere in my Surface class?

[Edited by - Mybowlcut on September 2, 2008 5:50:55 AM]

Advertisement
Quote:s1's surface screws up after that block executes... so I have issues somewhere in my Surface class?


It looks like the reference-counting isn't working properly. Can you post the code for Surface's second constructor?

Also, did you try stepping through this code snippet?

This topic is closed to new replies.

Advertisement