V
V
Vladimir Korshunov2021-10-26 15:25:05
C++ / C#
Vladimir Korshunov, 2021-10-26 15:25:05

Why does unordered_map stop working after calling the destructor?

I have this class:

class accauntant: public position
{
private:
    unordered_map<string, float> stakes;
public:
    ~accauntant() {stakes.clear(); name.clear(); surname.clear(); patronymic.clear(); salary = 0;}
    float getStake(position employee)
    {
        if(stakes.find(employee.getName() + employee.getSurname() + employee.getPatronymic()) != stakes.end())
            return stakes[employee.getName() + employee.getSurname() + employee.getPatronymic()];
        else
            return 0;
    }
    void setStake(position employee, float f)
    {
        stakes[employee.getName() + employee.getSurname() + employee.getPatronymic()] = f;
    }
    void addEmp(position employee)
    {
        stakes[employee.getName() + employee.getSurname() + employee.getPatronymic()] = 1;
    }
    float calculateSalary(position employee)
    {
        return stakes[employee.getName() + employee.getSurname() + employee.getPatronymic()] * employee.getSalary();
    }
};

Here is the position class:
class position
{
protected:
    string name, surname, patronymic;
    int salary;
public:
    position(){salary = 0;}
    position(string n, string s, string p, int sal)
    {
        name = n;
        surname = s;
        patronymic = p;
        salary = sal;
    }
    string getName() const
    {
        return name;
    }
    string getSurname() const
    {
        return surname;
    }
    string getPatronymic() const
    {
        return patronymic;
    }
    int getSalary() const
    {
        return salary;
    }
    void setName(string n)
    {
        name = n;
    }
    void setSurname(string s)
    {
        surname = s;
    }
    void setPatronymic(string p)
    {
        patronymic = p;
    }
    void setSalary(int s)
    {
        salary = s;
    }
};

If I explicitly call the destructor of an object of class accauntant, then when I call the addEmp function (I previously refilled the argument of this function with values), the program crashes with an error. If you do not clear the unordered_map with clear, then nothing works anyway. If you do not call the destructor of the argument of this function, but call the destructor of the accauntant class object, then nothing works either. Apparently the problem is in the accauntant destructor. You can not call the destructor by task, everything works without it, but still I would like to understand what exactly the problem is. Error: Thread 1: EXC_BAD_ACCESS (code=1, address=0x0).

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Mercury13, 2021-10-26
@GashhLab

As in a joke (obviously from the 70s, when there was a Football war between Honduras and El Salvador).
“Something worries me about Honduras.
- So don't scratch it.
Calling the destructor will cascade the destructors of all fields, and this will lead to the fact that in place of u_m there will be garbage like nullptr and dangling pointers.
Autodestructors are the most important feature of C++, which distinguishes it from other languages, and an explicit destructor call is rarely needed when we want a little more manual memory management. Here is an example from our project. There is a allocated piece of memory, and we want to destroy the object there and create a new one in its place, without giving back-allocating memory. We assume that the objects are the same, otherwise all the magic will be lost.

wiTile->~WiTile();
new (wiTile) WiTile(client(), icons, clazz, tileSettings(i));

In a project with hundreds of thousands of lines, I found 34 such places in libraries, mainly RapidJson, and three places in the program itself.
If you want to clear it completely, make a separate clear function.
By the way, in your case, an explicitly written destructor is not needed at all. The automatically called field destructors will do whatever it takes to destroy it gracefully.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question