Sunday, September 23, 2018

Memory leaks again when clearing memory

Arhhh! Memory leaks again?!! How many times I have been working on this shit? It has been reviewed and rework for many, many times. Remember, there are once a major changes to the code, most probably I miss out to run the unit test on Windows then now all test cases were failed in clearMemory(). For now I am reworking on the logic for clearing the memory, I hope this is the last and final.

FileBot::~FileBot()
{
if( localList.size() != 0 )
clearMemory(&*localList.begin(), localList);
if( remoteList.size() != 0 )
clearMemory(&*remoteList.begin(), remoteList);
}
void FileBot::clearMemory(FileNode *fileNode, FileNodeListType &fileList)
{
if( fileNode->sibling.size() == 0 )
return;
BOOST_LOG_TRIVIAL(info) << "Processing cleaning on: " << fileNode->getName() << " [" << fileNode << "] sibling : " << fileNode->sibling.size();
// there are sub directory contain in this directory
if( fileNode->sibling.size() > 0 ) {
for( FileNodeListType::iterator it = fileNode->sibling.begin(); it != fileNode->sibling.end(); it++ ) {
clearMemory(&*it, fileList);
}
}
BOOST_LOG_TRIVIAL(info) << "cleaning sibling memory on: " << fileNode->getName() << " [" << fileNode << "]";
fileNode->sibling.erase_and_dispose(fileNode->sibling.begin(), fileNode->sibling.end(), DisposeFileNode());
if( fileNode->getParentNode() == nullptr ) {
BOOST_LOG_TRIVIAL(info) << "cleaning root node: " << fileNode << " node name: " << fileNode->getName();
fileList.erase_and_dispose(fileList.begin(), fileList.end(), DisposeFileNode());
}
}
view raw FileBot.cpp hosted with ❤ by GitHub

No comments: