Heap corruption at program termination

199 views Asked by At

I can't figure out what is causing this memory leak (I assume that's what's going on.)

I've got several classes, so I'll post the constructors and destructors of each. The error message says the corruption is caused by the memory allocated at stock.cpp(20) which is the line

symbol(new char[]),

The constructors and destructors are as follows

hashmap.cpp

HashMap::HashMap(int capacity) :
  slots(new Slot[capacity]), 
  capacity(capacity), 
  nStocks(0),
  isEmpty(true)

HashMap::~HashMap(void)
{
    delete[] slots;
}

stock.cpp

Stock::Stock(char const * const symbol, char const * const name, int sharePrice, Date priceDate):
  symbol(new char[strlen(symbol)+1]),
  name(new char[strlen(name) + 1]),
  sharePrice(sharePrice),
  priceDate(priceDate),
  isEmpty(false)
{
  strcpy(this->symbol, symbol);
  strcpy(this->name, name);
}

Stock::Stock(void) :
  symbol(new char[]),  //this is where the memory allocation that causes the error occurs
  name(new char[]),
  isEmpty(true)

Stock::~Stock(void)
{
  delete[] symbol;
  delete[] name;
}

As you can see, the array that is allocated at stock.cpp(20) is destroyed when the destructor is called. So what's going on?

EDIT: Here is the complete error message

Debug Error!

Program: asnmt03/Debug/asnmt03.ext

HEAP CORRUPTION DETECTED: after Normal block (#168) at 0x004E51F8 CRT detected that the application wrote to memory after end of heap buffer

Memory allocated at stock.cpp(20)

0

There are 0 answers