-
vectors of pointers
I am getting memory leak errors when running my program.. I have determined that the error is in this section, specifically in the push_back line for the vector. the newCard variable receives the pointer to the Card object just fine. The problem occurs when I try to push this pointer onto the vector. the vector is part of the class this funtion belongs to,a nd is defined as vector <Card*> guardiansHand. Any input or information would be appreciated. thanks!
Code:
for ( int x = 0; x < drawNumber; x++ )
{
Card* newCard = new Card;
cardIndex = ( rand( ) % theDeck->getDeckSize( ) + 1 );
cout << cardIndex << endl;
theDeck->findACard( cardIndex );
newCard = theDeck->findACard( cardIndex );
guardiansHand.push_back( newCard );
newCard->printCardInfo( );
}
-
You create a new Card object and assign that to the pointer newCard but then you later reassign this pointer to the value returned from the theDeck->findACard( cardIndex ) function call and push this new value onto the vector. What happens to the memory that you got from the original call to new?
-
That is a valid point, and I have to admit that I don't completely understand pointers... in that previous code example, I allocated space for the newCard pointer, and then assigned the data in a different pointer to the newCard pointer, and deleted the old pointer. (this is done behind the scenes of the code section I have shown) However, the problem is identical when I try to use only the call to the findACard( ) function (which returns a pointer to a Card object) and don't deal at all with the newCard pointer.
It is possible I didn't completely understand your question, but I feel the problem lies in a diff. direction.
Code:
for ( int x = 0; x < drawNumber; x++ )
{
//Card* newCard = new Card;
cardIndex = ( rand( ) % theDeck->getDeckSize( ) + 1 );
cout << cardIndex << endl;
//newCard = theDeck->findACard( cardIndex );
guardiansHand.push_back( theDeck->findACard( cardIndex ) );
//newCard->printCardInfo( );
}
-
Another note on that issue is that the program works perfectly when I use the newCard pointer by itself, and simply output the value to the screen. The problem occurs when i try to push the newCard value onto the vector.
-
Here is the other function referenced in my code...
Code:
void Hand::drawCards( Deck* theDeck, int drawNumber )
{
static int init = 1;
int cardIndex;
if ( init == 1 )
{
srand( time( NULL ) );
}
for ( int x = 0; x < drawNumber; x++ )
{
cardIndex = ( rand( ) % theDeck->getDeckSize( ) + 1 );
cout << cardIndex << endl;
guardiansHand.push_back( theDeck->findACard( cardIndex ) );
}
and the findACard( ) function in the Deck class:
Code:
Card* Deck::findACard( int cardIndex )
{
return guardiansDeck[ cardIndex ];
}
guardiansDeck is assigned with vector<Card*> guardiansDeck; it is a private member of Deck. findACard is basically just a "getter" function for a specific item within a vector of Card pointers. The purpose of the drawCard function is to get a pointer from within the Deck vector, and push it onto the Hand vector. Since they are both pointers to the same location in memory, I shouldn't need to reallocate the memory, right?
If i just comment out the line that pushes the pointer onto the vector, it runs perfectly...
I can put in a line, such as
Code:
Card* newCard = theDeck->findACard( cardIndex );
and everything works fine. It only encounters problems when i push it onto the vector.
As far as deleting pointers, and freeing memory... I do have functions to do that once I am done with the pointer... but the program doesnt even get that far...
-
In case anyone reads this in the future and wants to help, I already solved the problem. Thanks :)