Sign in to follow this  

Editing a class' variables results on crash?

This topic is 4249 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Sorry for the dodgy title, but I can't think of a better way to put. I'm confused here. >_> Been flunking around with OpenGL making a game and... in one function, when I have a class edit it's own variable it crashes. But only in that one function. Well, here's the code incase my description isn't accurate enough (which I doubt it is). cGUI def:
class cGUI{

   private:

   int bX, bY;

   public:

   cGUI();
   ~cGUI();

   void onMouseDown();
   void onMouseUp();
   void onMouseMove(int,int);

   int mX, mY;
};
gui.cpp:
cGUI::cGUI()
{
   //mX = mY = 0;
}

cGUI::~cGUI()
{

}

void cGUI::onMouseDown()
{

}

void cGUI::onMouseUp()
{
//bX = 0;
}

void cGUI::onMouseMove(int Tx, int Ty)
{
   bX = 0;
}
The problem's in onMouseMove. Changing any public/private member of its own class results in the client crashing. Same thing happens in onMouseUp, too... but it works fine for the constructor. Oh, and most of those ints in cGUI are just for testing to see if I could get this thing working. Been at it an hour (since I'm trying to teach me to help myself), but I've given up on that and have decided to ask for some help. So any and all input is appreciated! -Etyrn Edit: Oh, and I forgot to add that calling other functions of it's own class, or any function for that matter works fine.

Share this post


Link to post
Share on other sites
In this case I would suggest breaking out a debugger and taking a look at the stack trace. Often times you *think* might be the error is just a symptom of a larger problem.

It's hard to put your problem into context since the class is pretty generic. Can you post some examples of how you are using it that causes it to crash?

Share this post


Link to post
Share on other sites
Hark, I'm dumb.

I had cGUI* client;
instead of cGUI* client = new cGUI;

So sorry for wasting your time. o.o
Though the debugger's still a good idea... I do need to learn how to use that. I'll try flunking around with that next time I get a problem instead of going here. Thankens.


-Etyrn

Share this post


Link to post
Share on other sites
You're propably calling the functions on an invalid object, eg. through a pointer that points to somewhere it shouldn't:

cGUI *gui;
gui->onMouseMove(1,2); // BOOM! Just garbage where gui points to

Share this post


Link to post
Share on other sites
Quote:
Original post by Etyrn
Hark, I'm dumb.

I had cGUI* client;
instead of cGUI* client = new cGUI;


I suspected something like this and that is why it's a good idea to initialize your pointers to NULL to make sure it's obvious that it hasn't been created yet.

I *think* MSVC will set uninitialized pointers to 0xdddddddd ( linux programmer here ) and you would have seen this in the debugger and figured out right away what the problem was.

The debugger is the most important tool you could ever use. Learning how to effectively use your tools is just as important as learning the language itself. It all comes after a bit of practice :).

Share this post


Link to post
Share on other sites
Quote:
Original post by Sharlin

cGUI *gui;
gui->onMouseMove(1,2); // BOOM! Just garbage where gui points to


What happens if you run code like this in debug mode?

Share this post


Link to post
Share on other sites
Quote:
Original post by acraig
I *think* MSVC will set uninitialized pointers to 0xdddddddd ( linux programmer here ) and you would have seen this in the debugger and figured out right away what the problem was.
Close. It's 0xcccccccc if the variable is on the stack (Which this pointer is), and 0xcdcdcdcd if it's on the heap. I think [smile]

Quote:
Original post by acraig
The debugger is the most important tool you could ever use. Learning how to effectively use your tools is just as important as learning the language itself. It all comes after a bit of practice :).
Seconded. Just play around with the debugger for a while, there's a lot of useful stuff there. And once you begin to remember the special values the debug CRT sets, things get easier (0xfdfdfdfd = end of allocated memory from new [probably malloc too], 0xfeeefeee = dead virtual function table, 0x00000000 = null pointer, 0xcccccccc and 0xcdcdcdcd = uninitialized memory, whatever freshly deleted memory is, etc).

Quote:
Original post by Fred304
Quote:
Original post by Sharlin

cGUI *gui;
gui->onMouseMove(1,2); // BOOM! Just garbage where gui points to


What happens if you run code like this in debug mode?
The same thing I'd imagine. Except you'll be able to get a stack trace and stuff in the debugger.

Share this post


Link to post
Share on other sites
Sign in to follow this