Sign in to follow this  
ehmdjii

problem with "bool" as class variable

Recommended Posts

ehmdjii    238
hello, i have the following class definition
#pragma once

#include <windows.h>
#include "glew.h"
#include "texture.h"


class button
{
	public:
		button() {};
		~button() {};
		void init(int xlu, int ylu, int xro, int yro);
		void render();
		void setTexture(texture* tex);
		
		float transparency;
		int xmin;
		int xmax;
		int ymin;
		int ymax;
		bool mouseover;
		bool mousedown;
		bool active;		

	private:
		texture* tex;
};

the problem are the three bool variables. they always seem to messup the following variables. in this case it is the tex variable (becomes nullpointer). if i move the bools up before the trasparency for example, this one is messed up (contains wrong values). if i change the bool to BOOL (notice the uppercase) which is defined as int by windef.h everything seems to work fine. what is the problem here and why cant i use normal c++ bools? thanks!

Share this post


Link to post
Share on other sites
Benden    1480
Define 'messed up'....

How are you using it, and from what point are the values messed?

I notice your constructor doesnt initialise the values at all so when are they all set?

Share this post


Link to post
Share on other sites
ehmdjii    238
thank you,

they are actually set in the init-function, but even if i initilaize them in the constructor nothing changes.

by messed up i mean as i have said above. if it is a pointer i get a reading-access-violation although i have definitely set the pointer before and for the primitve types such as float or int they contain random values.

Share this post


Link to post
Share on other sites
Simian Man    1022
More than likely, you are either not initializing them (as Benden said) or you have memory corruption going on. Post the constructor and the code that writes to the tex variable.

Share this post


Link to post
Share on other sites
ehmdjii    238
thank you, but even if i initialize them in the constructor like this, the problem persits.

button() {
transparency = 0.5;
mouseover = false;
mousedown = false;
active = false;
xmin = 0;
xmax = 0;
ymin = 0;
ymax = 0;
};



what is really strange about this, is as i wrote above, as soon as i change the bool to BOOL everything works fine.

thanks!

Share this post


Link to post
Share on other sites
Sneftel    1788
So just put a data breakpoint on one of the other variables that gets messed up and see what's going wrong.

Share this post


Link to post
Share on other sites
Benden    1480
Quote:
Original post by ehmdjii
thank you, but even if i initialize them in the constructor like this, the problem persits.
*** Source Snippet Removed ***

what is really strange about this, is as i wrote above, as soon as i change the bool to BOOL everything works fine.

thanks!


Quick couple of comments, use the initialiser list rather than setting them in the constructor body like that. Also its best to init them in order of definition in the class. Remember for completeness to init tex to NULL as it is a pointer.

Once you've done all that stick a breakpoint just after you create an instance of your button class and check all the member vars. You are checking them in debug and not release arent you?

Share this post


Link to post
Share on other sites
Simian Man    1022
Quote:
Original post by ehmdjii
what is really strange about this, is as i wrote above, as soon as i change the bool to BOOL everything works fine.


I guarantee you that, though changing to BOOL may fix these specific *symptoms* of the problem, it will not fix the problem itself. Like others have said, step through your code with a debugger and especially watch where you access the pointer tex. If it is a dynamic array, it is likely that you are corrupting your other members through it.

Share this post


Link to post
Share on other sites
ehmdjii    238
thank you everyong for your help.

yes it seems that the problem is elsewhere, cause it looks like the instance that is accessed later on when the problem occurs, is a complete different one.

but how can this happen? how can an instance be created without passing through the constructor?

i am strong the instances of the button class in an array and access them only via this array:
button b0, b1, b2, b3, b4, b5, b6, b7;
button buttonArray[8] = {b0, b1, b2, b3, b4, b5, b6, b7};


Benden, thanks for your adivces. i am now initializing also the tex-pointer to NULL. yes, i am debugging in DEBUG mode.

Simian Man, thanks. yes i am going through the code step by step but i still couldnt find the point where the data gets corrupted.

Share this post


Link to post
Share on other sites
Sneftel    1788
Quote:
Original post by ehmdjii
yes it seems that the problem is elsewhere, cause it looks like the instance that is accessed later on when the problem occurs, is a complete different one.

but how can this happen? how can an instance be created without passing through the constructor?

It's likely that the object is being copy constructed. Nevertheless, you are fifty seconds away from the compiler just telling you exactly where the problem is. Just use a data breakpoint.

Share this post


Link to post
Share on other sites
dmatter    4844
Quote:
Original post by ehmdjii
button b0, b1, b2, b3, b4, b5, b6, b7;
button buttonArray[8] = {b0, b1, b2, b3, b4, b5, b6, b7};

That creates 8 instances of buttons using your default constructor then copy-constructs, with an implicit copy constructor, 8 more instances into an array.

You probably just want:

button buttonArray[8];

That will instantiate an array of 8 default constructed buttons.

Share this post


Link to post
Share on other sites
ehmdjii    238
it's not. :) sorry

ok, i found out what a data breakpoint is, used it and it turns out that the data is modified in another class's constructor during an fread.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by ehmdjii
thank you,

excuse the stupid question, but what is a "data"-breakpoint? :-)
In VC2005 (It'll be similar for other Visual Studio versions), put a breakpoint just after where you construct the variable, then when it's hit go to Debug -> New Breakpoint -> New Data Breakpoint, put in "&mouseover" for the address, and hit OK.
Then you'll break to the debugger whenever that variable is changed.

Share this post


Link to post
Share on other sites
Evil Steve    2017
Quote:
Original post by ehmdjii
it's not. :) sorry

ok, i found out what a data breakpoint is, used it and it turns out that the data is modified in another class's constructor during an fread.
It sounds like you're reading too many bytes and overflowing whatever buffer you're writing into.

Share this post


Link to post
Share on other sites
ehmdjii    238
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.

Share this post


Link to post
Share on other sites
Sneftel    1788
Now, the caveat there is that if you're later deallocating that memory, it may just happen to be in the same location as the eventual error location. To make sure that this isn't the case, use _CrtSetDbgFlag() to set the _CRTDBG_DELAY_FREE_MEM_DF flag. Alternately, simply keep running through the program, and see where the last modification of that memory location happens before the error occurs.

Share this post


Link to post
Share on other sites
ehmdjii    238
thank you, sneftel.

the last modification of the variable with the data breakpoint also happens in that fread line.

also, the heap-debug using _CrtSetDbgFlag does not output anything. or how is it supposed to notify you if such an overwrite occurs?

Share this post


Link to post
Share on other sites
ehmdjii    238
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.

Share this post


Link to post
Share on other sites
ehmdjii    238
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.

Share this post


Link to post
Share on other sites
ehmdjii    238
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.

Share this post


Link to post
Share on other sites
ehmdjii    238
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!

Share this post


Link to post
Share on other sites
rip-off    10979
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this