Archived

This topic is now archived and is closed to further replies.

this pointer troubles

This topic is 5377 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

Okay, i have the following code:
  
if(myMove.empty()) {
			this->animating = false;
			this->myMove.clear();
			CC_Piece * tempThis = this;
			this->endCell->setPiece(tempThis);
		}
  
When i run the program, the first line in setPiece(CC_Piece *) crashes. I assume it has something to do with my calling the setPiece function and passing it the "this" pointer. Here is where it crashes:
  
void CC_Cell::setPiece(CC_Piece * pPiece) {
	this->cellPiece = pPiece;
	this->cellPiece->setWindowX(this->mWindowX);
	this->cellPiece->setWindowY(this->mWindowY);
}
  
any help???

Share this post


Link to post
Share on other sites
My first suggestion is to get rid of all the "this->". They are never needed and they just clutter up the code (unless you are trying to do C++ stuff with a C compiler). If you think they are needed, then you are doing something wrong.

As far as your crash... it crashes, oh joy.

(sorry for the whining)

Why do some people think that other people have fully-featured development systems embedded in their brains? How can anyone possibly know the answer to "why doesn''t this compile"? "why doesn''t this link?", "why doesn''t this work?", and "why does this crash?", when all that is provided is a couple lines of source code????

At least provide the error message!!! At least describe what it is supposed to do, and what it is doing or not doing!!!!
...

My GUESS is that "endCell" is 0, so when you call setPiece(), "this" is 0.

Share this post


Link to post
Share on other sites
1) I know they are not needed, its personal preference i guess and I''m pretty sure it doesnt affect performance.
2) The error message is:
quote:

Unhandled exception at 0x00455329 in Chinese Checkers.exe: 0xC0000005: Access violation writing location 0xfdfdfdfd.


0xfdfdfdfd is the address of endCell in the source below. It is not set to 0. My main question is has anyone ever had problems passing the "this" pointer to a member of a class. That is what seems to be the problem. When I do not use the this pointer it works ( i use this function elsewhere in the code ) . . . sorry to assume that everyone has a fully featured development system embedded in their head. Any help is appreciated.

Share this post


Link to post
Share on other sites
looks like a debugger initialized pointer. do you realize what the this pointer is?


AClass * Testing = (AClass*)0x12345678;
Testing->AMethod { this == 0x12345678; /* true */ }


you don't need to put this-> in front of everything, the compiler does that automatically. there is ZERO difference between what you did and if you don't have this-> on everything. all it does is clutter. somewhere, you aren't intialializing a pointer. basically, you created a pointer somewhere like this:

CC_Cell * ACell;

the debugger, since you didn't set this to any value specifically, sets this to its debug value, in your case 0xFDFDFDFD. if you happened to replace that line with CC_Cell * ACell = 0;, you would get the same error but 'Access violation writing location 0x00000000'

[edited by - billybob on March 23, 2003 5:30:42 PM]

Share this post


Link to post
Share on other sites
thank you for your help, turns out i was grabbing the pointer to a CC_Cell object from a vector i was using to store CC_Cell*''s. Problem was that there is one case where the vector would get emptied before I grabbed the pointer . . . then when i called vector::back, it was returning something useless.

thanks again, and again i realize that the "this" pointer is not needed . . . guess i should go through and take it out, it doesnt bother me though.

Share this post


Link to post
Share on other sites
quote:
Original post by mstein
My main question is has anyone ever had problems passing the "this" pointer to a member of a class.


"this" is not really any different from any other pointer. However, you probably want to avoid doing this:

delete this;

Share this post


Link to post
Share on other sites