I want to read binary tree from file. I have written a method.
BinaryTree * FileService::read(BinaryTree * node) {
basic_string<char> np = "nullptr";
char * val = new char ();
if (!fscanf(this->filePtr, "%s ", val)) {
delete val;
return node;
}
string str(val);
if (str == np) {
delete val;
return node;
}
node = new BinaryTree();
if (Helper::isFloatNumber(val)) {
node->setNumericValue(stof(val));
} else {
node->setStrValue(val);
}
delete val; <----HERE SIGTRAP (Trace/breakpoint trap)
node->setLeft(this->read(&node->getLeftReference()));
node->setRight(this->read(&node->getRightReference()));
return node;
When I execute my code, I get an error Process finished with exit code -1073740940 (0xC0000374)
When I try to debug I receive this one SIGTRAP (Trace/breakpoint trap)
where last delete val
is. When I comment this line my code works well. But I think memory leak will be there in this case. Tell me please where am I wrong? And how can I change my code to avoid memory leak and exceptions?
I tried to move line delete val;
before last return. It didn't fix. I tried to make condition
if (val != nullptr) {
delete val;
val = nullptr;
}
instead every delete val;
result was the same.