I have an abstract class named Company
, a typedef typedef Company* CompanyP;
and the following map:
map<string, CompanyP> companies;
When I add companies into the program it's using the insert
and make_pair
functions, like this:
companies.insert(make_pair(companyName, new Stock(...)));
(Stock
's base class is Company
)
So as far as I understand the second
part of the pair is holding a pointer to this object that i allocated using new
, so I should be able to delete
it in later part of the code.
So why when I get to the following code in a different function called
removeCompany
, I get a runtime error? (compList
is an array of strings and holds the keys to all existing functions) :
// Delete the company and than delete the pointer from the map
cout << companies.find(compList[cmd - 1])->first << endl;
delete companies.find(compList[cmd-1])->second;
cout << "The company \"" << compList[cmd-1] << "\" has been removed." << endl;
companies.erase(compList[cmd - 1]);
I know that the key is correct using the first line with the cout
command because it rightfully shows that the find
function indeed finds the correct pair, but when the debugger gets to the delete command I get a runtime error _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)
.
I've looked into other questions but they were all examples of using the wrong pointers. Here i'm using a base class pointer that points on a derived class, could this be the problem?