Answer the question
In order to leave comments, you need to log in
How to avoid a memory leak in a class with a field pointer to its stream?
In the class, a method is launched in a separate thread, which should take care of releasing resources, but I cannot free the pointer to the thread itself.
The class looks like this
class Listener{
private :
std::thread th; // указатель на поток, не могу его удалить с кучи
char *str; // удалятся
public:
Listener(char *_str,int len){
str=new char[len];
strncpy(str,_str,len);
th=new std::thread(run,this); // запускаю выполнение
}
static void run(Listener *_this){
/* body
*/
delete [] _this->str; // удаляю строку
// delete _this->th; // ОШИБКА критическая, если добавить
delete _this; // вызываю деконструктор и удаляю this : Listner
}
};
void main(){
for(;;){
Listener *l=new Listener("12345678912345");
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question