I get sigtrap (trace/brakepoint trap) error in my code c++. I have a generic list that I use for storing teams. I have one place where I use dinamic memory allocation. My list:
#ifndef LISTOFITEMS_HPP
#define LISTOFITEMS_HPP
#include <iostream>
template <class T>
class Csapatok
{
struct ListaElem
{
T adat;
ListaElem *kov;
};
ListaElem *elso;
public:
Csapatok() : elso(nullptr) {}
void beszur(const T& dat)
{
ListaElem *p = new ListaElem;
p->adat = dat;
p->kov = nullptr;
if(elso == nullptr)
{
elso = p;
}
else
{
ListaElem* akt = elso;
while (akt->kov != nullptr)
{
akt = akt->kov;
}
akt->kov = p;
}
}
void torol(const T dat)
{
ListaElem* aktualis = elso;
ListaElem* elozo = nullptr;
if (aktualis != nullptr && aktualis->adat == dat)
{
elso = aktualis->kov;
delete aktualis;
return;
}
while (aktualis != nullptr && aktualis->adat != dat)
{
elozo = aktualis;
aktualis = aktualis->kov;
}
if(aktualis == nullptr)
{
return;
}
if (elozo == nullptr)
{
elso = aktualis->kov;
}
else
{
elozo->kov = aktualis->kov;
}
delete aktualis;
}
bool keres(T dat) const
{
if(elso->adat == dat)
return true;
ListaElem *p = elso;
while (p->kov != nullptr)
{
if(p->adat == dat)
return true;
p = p->kov;
}
return false;
}
};
#endif //LISTOFITEMS_HPP
And this is my code for the Teams class:
#ifndef EGYESULET_H
#define EGYESULET_H
#include <iostream>
#include <cstring>
class Csapat{
protected:
size_t alapletszam = 10;
size_t letszam =0;
char *csapetnev;
public:
Csapat() :csapetnev(nullptr) {}
Csapat(const char* csnev) {
if (csnev != nullptr) {
csapetnev = new char[strlen(csnev) + 1];
strcpy(csapetnev, csnev);
} else {
csapetnev = nullptr;
}
}
const char* getcsnev() { return csapetnev;}
~Csapat()
{
delete[] csapetnev;
std::cout << "csapat dtor"<< std::endl;
}
};
#endif //EGYESULET_H
I have 3 derived classes from Teams (like basketball team, football team etc...) but they are so simple I dont put it here.
I have a testing file where I simply create an football team object f1. Then I put this team in the list using "beszur" function, and then checking it with "keres" if it is in or not. Somewhy it gives false when it should be true because I can see it in the debugger that it is in the list in its elso node. Then I try to delete it with "torol". Thats when I get a SIGTRAP error in the debugger at the line "delete[] csapatok".
I assume theres something with the destructors but I just cant handle it.