aclements / libelfin

C++11 ELF/DWARF parser
MIT License
317 stars 99 forks source link

elf: fix circular dependency in elf object #30

Open abenkhadra opened 6 years ago

abenkhadra commented 6 years ago

object elf (parent) owns the memory of its children, namely, sections and segments. However, instances of the latter share memory ownership of the parent elf due to its pimpl implementation using shared_ptr. This creates a circular dependency that prevents cleaning the memory acquired by elf objects.

I break this circular dependency through the standard mechanism of weak_ptr. Basically, the children now own a weak_ptr to the implementation of their parent elf object. They instantiate an instance of the parent only when an action involving the parent is needed.

axel-capodaglio commented 9 months ago

This can also be solved by just changing "const elf f" member of segment and section to "const elf& f". Because the elf keeps the children, they are alive as long as the parent is alive (obviously with this solution copies of segments and section must be in scope with the parent elf object).