problem with "bool" as class variable

Started by
30 comments, last by Zahlman 15 years, 12 months ago
Quote:Original post by Evil Steve
It sounds like you're reading too many bytes and overflowing whatever buffer you're writing into.


thank you.

this is the code where the data-breakpoint is triggered
	size_t bytestoread = width * height * depth;	size_t bytesread = 0;	pixels = (GLubyte * )malloc(bytestoread);	bytesread = fread(pixels, 1, bytestoread, filepointer);	if(bytesread != bytestoread) {		err = "Byte read mismatch";	}


the err is never reached though, so it seems as if i am reading the correct amount of memory i have previously allocated.
Advertisement
Quote:Original post by Evil Steve
It sounds like you're reading too many bytes and overflowing whatever buffer you're writing into.


thank you.

this is the code where the data-breakpoint is triggered
	size_t bytestoread = width * height * depth;	size_t bytesread = 0;	pixels = (GLubyte * )malloc(bytestoread);	bytesread = fread(pixels, 1, bytestoread, filepointer);	if(bytesread != bytestoread) {		err = "Byte read mismatch";	}


the err is never reached though, so it seems as if i am reading the correct amount of memory i have previously allocated.
Quote:Original post by Evil Steve
It sounds like you're reading too many bytes and overflowing whatever buffer you're writing into.


thank you.

this is the code where the data-breakpoint is triggered
	size_t bytestoread = width * height * depth;	size_t bytesread = 0;	pixels = (GLubyte * )malloc(bytestoread);	bytesread = fread(pixels, 1, bytestoread, filepointer);	if(bytesread != bytestoread) {		err = "Byte read mismatch";	}


the err is never reached though, so it seems as if i am reading the correct amount of memory i have previously allocated.
is it possible, that the order of posts got changed in this thread?

besides that, i still couldnt find a solution for my problem. why are my variables being overwritten during a correct fread operation and furthermore why does this not happen when i change the variables form bool to BOOL?

thanks!
BOOL is (AFAIK) a Windows typedef of an integer. On 32bit systems, integers are typically 4 bytes long. A "bool" is (IIRC) the same as a char, 1 byte long. This difference affects how everything is packed and padded in memory.

What is probably happening is that something else is getting overwritten, you just haven't noticed it yet. As others have said, this is curing the symptoms, not the disease.

I don't know if we have enough context to diagnose this. Is the fread call inside a member function? You might be calling said function on a uninitialised pointer. The fact that you hadn't set your "tex" member to NULL when you first posted indicates that this might be a typical occurrence in your code. If you could post more code, perhaps we might be able to help. Other than that, I would suggest this as an excellent opportunity to learn how to use your debugger.
thanks for your help.

yes, the fread is part of a member function of another class. it is actually in the contructor of the texture class.

here is the full code:

texture::texture(char *filename){	error = false;		FILE* filepointer;	filepointer = fopen(filename,"rb");	if(filepointer == 0) 	{		err = "Cannot load following texture:\n";		err.append(filename);		error = true;		return;	}		byte header[18];	fread(header, 18, 1, filepointer);	width = header[13]*256 + header[12];	height = header[15]*256 + header[14];	depth = header[16] / 8;	size_t bytestoread = width * height * depth;	size_t bytesread = 0;	pixels = (GLubyte * )malloc(bytestoread);	bytesread = fread(pixels, 1, bytestoread, filepointer);	if(bytesread != bytestoread) {		err = "Byte read mismatch";		error = true;	}	for(GLuint cswap = 0; cswap < width * height * depth; cswap += depth) {		pixels[cswap] ^= pixels[cswap+2] ^=		pixels[cswap] ^= pixels[cswap+2];	}	fclose(filepointer);}
Quote:Original post by ehmdjii
i still couldnt find a solution for my problem. why are my variables being overwritten during a correct fread operation and furthermore why does this not happen when i change the variables form bool to BOOL?

Here's my guess.

You create your button object, and set a watch on a data member.

You then copy your button object (without having defined an appropriate copy constructor) and destroy the original button object.

Later, you malloc() some buffer and perform an fread() on that buffer. The watch is still in effect on that memory, and you get dropped into the debugger as it's triggered. Boom

Here's my suggestion: create the appropriate copy constructor for your class. The fact that you've (possibly) changed sizes of some data members can affect where the symptom of your problem shows up, but does not make the problem go away. Your problem actually appears to be that you don't understand the C++ object model and associated object lifetime issues. That's a very easily solved problem.

Try creating a copy constructor, and assignment operator, and a destructor and put print statements in them (or debugger breakpoints) to get a better idea about where your objects are being created, copied, and destroyed.

-- smw

Stephen M. Webb
Professional Free Software Developer

thank you!

yes it seems that the copy constructor is called here in the second line:

button b0, b1, b2, b3, b4, b5, b6, b7;
button buttonArray[8] = {b0, b1, b2, b3, b4, b5, b6, b7};

why is that?
Quote:Original post by ehmdjii
it seems that the copy constructor is called here in the second line:
button b0, b1, b2, b3, b4, b5, b6, b7;button buttonArray[8] = {b0, b1, b2, b3, b4, b5, b6, b7};

why is that?

Because you are creating new instances of your button class in the array by copying the values in the array's initializer list.

It sounds like you are more familiar with the Java object model (in which all object instances are created on "the heap" and code only ever manipulates pointers to the instances) than the C++ object model, where object instances can be created in free store ("on the heap"), in automatic storage ("on the stack") or in static storage ("globals" or "statics").

Your code does not move the button instances into the array. It copies them. That means using the copy constructor (or, if the target is already constructed, the assignment operator).

So, make your copy constructor do the right thing. Then check your destructor to make sure it does the right thing (when is your texture freed?). Check that your assignment operator does the right thing. Learn the rule of three.

Stephen M. Webb
Professional Free Software Developer

A couple thoughts -- Generally, when I see this type of corruption my first thoughts are: The copy constructor or assignment operator are incmplete/wrong or I've assigned into a variable after casting it to some other type.

Next thought -- since you mentioned fread and assuming you're just fread'ing several contiguous pieces of data from a file in one go, then you must understand the actual layout of your object data in memory. When your compiler decides the layout of your object's variables in memory it has the freedom to insert padding between data to preserve natural alignment. In other words, this struct:

struct somestruct {  char  a;  short b;  char  c;  int   d;};

might actually end up looking like this in memory:
member | size | byte address offset-----------------------------------     +-+-+----+     a |    1 | 0x0                     |a|*| b  | empty |    1 | 0x1                     +-+-+----+     b |    2 | 0x2                     |c|  *   |     c |    1 | 0x5                     +-+------+ empty |    3 | 0x3                     |   d    |     d |    4 | 0x8                     +--------+


To eliminate this packing, you need to tell your compiler to disable packing for this object. In visual studio, you use the '#pragma pack" family of commands -- which you can read about on MSDN.

throw table_exception("(? ???)? ? ???");

This topic is closed to new replies.

Advertisement