_BLOCK_TYPE_IS_VALID(pHead-nBlockUse)

Started by
4 comments, last by _ArmuT_ 16 years, 3 months ago
Hi guys, I spent hours and hours trying to figure out what is really happening with my code. Here is the scenerio: I have three classes. ClassA, ClassB and ClassC. ClassB stores an array of pointers to ClassA. It is a composition. ClassB is responsible of ClassA's birth and death. ClassC wants to use and store some of the ClassA objects stored in the ClassB. ClassB has an accessor function, returning a pointer of ClassA. So what I do is taking that pointer from ClassB and passing it to ClassC. Everything seems to be working. Output is allright. But when the program ends, destructors called, I got this error: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) I guess ClassB is destroying the ClassA objects so there is nothing left for ClassC's destructor to delete, what I mean is double delete makes compiler generate this error? But I am not sure if this is the case, or how to overcome it? Any ideas ? Thanks.
Advertisement
The solution is to do as you said; let ClassB be responsible for the construction and destruction of ClassA. Since ClassC only gets references to existing objects that some other class already is responsible for, ClassC should not delete them.
Alternately you can use a reference counted smart pointer like boost::shared_ptr and pass those around.
@Lajnold : The thing is ClassC is storing the pointers passed to them in an array of pointers. And this array is dynamic and created by new. For every new operator shouldn't I be using delete or/and delete[] ?

//ClassC, mHandptr is a private member variable to card pointers,//card** mHandptr;void hand::addCard(card* theCard){    if (theCard != NULL) {				if (mHandptr== NULL) {			mHandptr = new card*[mCardsHolding];			mHandptr[mCardsHolding++] = theCard;		}		else {			card** temp = mHandptr;			mHandptr = new card* [mCardsHolding];			memcpy(mHandptr,temp, mCardsHolding * sizeof(card*));			mHandptr[mCardsHolding++] = theCard;		}    }}


And this is from my ClassB:

card* deck::dealOne(void) {	if (mNotDealt!= 0 )return mSuitptr[mCardCount - mNotDealt--];	else return NULL;}


As it is clear now, I am trying to represent a card game by these three classes. ClassA is the card, ClassB is the deck, ClassC is the hand.

@SiCrane: I am going to explore that shared pointer concept BUT for the learning purposes I want to know everything about pointers, references, etc.. before moving on to life saver, proven to be error prone containers.

And most importantly,
Thank you guys!
What happens to the original data held by mHandptr in that code? Also, you might want to just throw all that memory manipulating gunk away and use a vector.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.
Quote:Original post by smart_idiot
What happens to the original data held by mHandptr in that code? Also, you might want to just throw all that memory manipulating gunk away and use a vector.


Original data is held by mSuitptr which is declared in ClassB( my deck class). ClassB is the one responsible of taking care of the stuff.
Below is the responsible destructor:
deck::~deck() {	for(int j=0; j<mCardCount; j++) delete mSuitptr[j];	delete[] mSuitptr;	cerr<< "-Deck Destructor-" << endl;}


I just couldn't figure out a way to make things work with this schema, what I mean is with shared pointers between classes.

And thank you for the advice, I am going to learn and use reliable containers such as vectors but I just want to learn more before using them




This topic is closed to new replies.

Advertisement