There is some issue with this container. It happens only on Windows, no problem in Linux at all. When removing an object from an intrusive container, make sure the nested intrusive container is clear off before the object is removed. I have run some test case about it.
Here is the declaration of the object for the test case, the sibling member is the recursive intrusive container:
class FileNode : public boost::intrusive::list_base_hook<link_mode<auto_unlink> > { public: boost::intrusive::list<FileNode, base_hook<list_base_hook<link_mode<auto_unlink> > >, constant_time_size<false> > sibling; ... ... };This is the intrusive list declaration:
typedef boost::intrusive::list<FileNode, base_hook<list_base_hook<link_mode<auto_unlink> > >, constant_time_size<false> > FileNodeListType; class FileBot { public: FileNodeListType fileList;First, an object was pumped into the intrusive container, fileList in this case. Then allocate another object into recursive list, sibling. I loop through every object in the fileList and put in another object into the recursive list.
void FileBot::addFileNode() { FileNode *f1 = new FileNode(); f1->setName("ASD"); fileList.push_back(*f1); } void FileBot::addSubFileNode() { FileNode *f2 = NULL; for(FileNode &T : fileList) { f2 = new FileNode(); f2->setName("QWE"); T.sibling.push_back(*f2); } }Now I want to clear everything in the fileList, without knowing is there any object saved into the container yet. Basically I just doesn't care at all, I just want to clear the container. Thus I implemented the following code:
void FileBot::clearMemory() { fileList.erase_and_dispose(fileList.begin(), fileList.end(), DisposeFileNode()); }When I call this method, memory leak is detected. This happens only on Windows. This is my mistake. I overlook on the addSubFileNode() on how I extract the object, T from the fileList. Each object has its sibling, and each sibling has another FileNode object in it, which consume memory as well. For my case, the object inside sibling memory is not removed but the object in fileList memory has already removed. Put in other words, it just like the parent object contain the child object, when releasing the memory, the child's object must release first then only release the parent's object. And what I did here is I go straight to the release the parent object, thus the child object is now complaining about the memory leak.
So in order to play safe in this game, I'll do this:
void FileBot::clearMemory() { for(FileNode &T : fileList) { T.sibling.erase_and_dispose(T.sibling.begin(), T.sibling.end(), DisposeFileNode()); } fileList.erase_and_dispose(fileList.begin(), fileList.end(), DisposeFileNode()); }Clean off the memory in the recursive intrusive container before cleaning the memory in the parent intrusive container. This should be the strategy for this game.
No comments:
Post a Comment