Why is my pile damaged?

I get an error - a lot of corruption, I can’t understand why.

My base:

h:

class Base
{
public :
    Base(char* baseName, char* cityName);
    virtual ~Base();

    list<Vehicle*>::const_iterator GetEndList();
    void PrintAllVehicles(ofstream &ResultFile) const;
    char* GetBaseName() const;
    char* GetLocation() const;
    void InsertNewVehicleToBase(Vehicle* newVehicle);
    list<Vehicle*>::const_iterator FindVehicle(char* id);
    void RemoveVehicle (list<Vehicle*>::const_iterator beg);



 private:
    char* m_name;
    char* m_location;
    list<Vehicle*> m_baseVehicles;

};  

cpp:

Base::Base(char* baseName, char* cityName)
{
    m_name = new char [strlen(baseName)+1];
    strcpy(m_name, baseName);
    m_location = new char [strlen(cityName)+1];
    strcpy(m_location, cityName);
}

Base::~Base()
{
    delete [] m_name;
    delete [] m_location;
    //m_baseVehicles.clear();
}

Armenian destructor:

Army::~Army()
{
    list<Base*>::iterator baseIter = m_basesList.begin();
    for (baseIter ; baseIter != m_basesList.end() ; ++baseIter)
        delete (*baseIter);
    m_basesList.clear();
 }  

What am I doing wrong?

+3
source share
6 answers

There is nothing wrong with the code that you showed, so there is a chance that there is an error in the code that you did not show.

, Base . , - Base, Base, , , , .

Army ( Base), , , .

, , Base. , Army, - Army? , , Base*, Army, , ?

+7

:

  • char * not std::string
  • STL
  • CRT ++
+7

. , , .

+5

, , . :

Base(char* baseName, char* cityName);

const char*, .

virtual ~Base();

, virtual; , .

list<Vehicle*>::const_iterator GetEndList();

const, const_iterator: list<Vehicle*>::const_iterator GetEndList() const;

char* GetBaseName() const;
char* GetLocation() const;

const char*, , .

list<Vehicle*>::const_iterator FindVehicle(char* id);

, const: list<Vehicle*>::const_iterator FindVehicle(char* id) const;

Base::~Base()
{
    delete [] m_name;
    delete [] m_location;
    //m_baseVehicles.clear();
}

m_baseVehicles.clear();, . , , .

:

"". Army?

Army::~Army()
{
    list<Base*>::iterator baseIter = m_basesList.begin();
    for (baseIter ; baseIter != m_basesList.end() ; ++baseIter)
        delete (*baseIter);
    m_basesList.clear();
 }  

m_basesList.clear();.

+3

. , ?

+1

, , . , .

, , , , Valgrind, .

+1

Source: https://habr.com/ru/post/1766780/


All Articles