I have classes Deck and PlayingCard. A Deck object must have a dynamically allocated array of pointers to PlayingCard objects:
PlayingCard** _playing_cards;
To initialise this array, the Deck's constructor and build() functions are called:
Deck::Deck(int size)
{
_total_playing_cards = size;
_deal_next = 0;
build();
}
void Deck::build()
{
_playing_cards = new PlayingCard*[_total_playing_cards];
for(int i = 1; i <= _total_playing_cards; ++i)
{
_playing_cards[i-1] = new PlayingCard(i % 13, i % 4);
}
}
Releasing the memory allocated with 'new' is handled in the destructor:
Deck::~Deck()
{
for(int i = 0; i < _total_playing_cards; ++i)
{
delete[] _playing_cards[i];
}
delete[] _playing_cards;
}
I then have a separate file, deck_test.cpp, which has a main() to simply construct and destruct a Deck object:
int main()
{
Deck deck(52);
deck.~Deck();
return 0;
}
This compiles fine, but when debugging, Visual Studio reports "Unhandled exception at 0x5ab159da (msvcr100d.dll) in Playing Cards.exe: 0xC0000005: Access violation reading location 0xfeeefee2." When looking at the call stack, the problem appears to be occurring where I use the 'delete[]' operator in the 'for' loop within the destructor. Is this not the correct way to release memory from an array of pointers?