I have been debugging this memory leak for hours, and just find out that there's a cyclic in the program.
The methods introduced on the Internet is to make shared_ptr a weak_ptr, but it's defined in this assignment that we should use shared_ptr.
Basically, the program is a file system. Like when you type cd ., you'll enter the current address.
currentAddress, which is an inode_ptr that points to the current address.directory, which is defined as map<string, inode_ptr<node>>. directory.first is the filename/directory name like ., .., or fileA And directory.second is an inode_ptr, same as currentAddress.So what I'm trying is to put pair<".", currentAddress> into directory.
And currentAddress should also point to directory
How to avoid memory leak in this case, but not to use weak_ptr? Thanks!
There is no way around using a weak pointer here. That's the reality here, it is what it is.
However, there are things that can be done here to minimize the use of a weak pointer, and mostly hide it as an internal implementation detail, and the users of your virtual filesystem will only see ordinary, strong pointers.
Begin by noting that each virtual directory always has an entry for "." and "..". This means that you do not need to store the actual entry for "." and ".." in your directory map. The directory map has only the entries for the actual contents of the directory.
You will then have to do some work in order to provide a custom iterator, or a facade of some kind that wraps the iterators around the directory map, and augments them with entries for "." and "..", synthesizing the corresponding inode_ptr<node>.
The users of your virtual filesystem would not access your directory container directly, it's a private implementation detail. Rather, you will provide the appropriate methods to construct your facade iterators, over a virtual container that includes all the entries from the underlying directory map, plus the on-the-fly synthesized entries for "." and "..".
To avoid cyclical references, each directory in your emulated filesystem will need to hold a weak pointer to its parent directory. There's no other way to avoid cyclical references. But this should be fine. As long as the parent directory exists, and holds a strong pointer to its subdirectory, the weak pointer from the subdirectory to the parent directory will remain valid. Your iterator/container facade, that augments the directory map by synthesizing the "." and ".." entries, will need to recover a strong pointer from the weak pointer, and return it as the supposed strong pointer assosiated with an entry for "..". Similarly, the facade for the directory map will construct a strong pointer to itself, and return it as the value for the "." entry.
The end result is that the users of your virtual filesystem still end up using regular, strong pointers, while under the scenes you will use weak pointers from directories to their parent directories in order to prevent circular references.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With