Minres / SystemC-Components

A SystemC productivity library: https://minres.github.io/SystemC-Components/
https://www.minres.com/#opensource
Apache License 2.0
81 stars 21 forks source link

Possible memory leak created by using find_or_create #32

Closed mslijepc closed 2 years ago

mslijepc commented 2 years ago

Hi,

I have run asan (but not on the original repo) and found possible memory leak in: https://github.com/Minres/SystemC-Components/blob/ad8aef0430a196df822740fa9984885553ce45f1/src/bus_interfaces/axi/pin/axi4_target.h#L250

fsm_handle object is created by calling find_or_create, but never deleted

mslijepc commented 2 years ago

Or the memory management is the responsibility of find_or_create ?

eyck commented 2 years ago

Indeed this is a memory leak. The fsm handles are created at https://github.com/Arteris-IP/tlm2-interfaces/blob/48e2de7033275bfc1807925e96154d43325a7325/axi/fsm/base.cpp#L53. Once the bus transaction is finished they are returned into a pool of handles (https://github.com/Arteris-IP/tlm2-interfaces/blob/48e2de7033275bfc1807925e96154d43325a7325/axi/fsm/base.h#L165) and reused from there. But this pool is not free'd upon destruction thus in principle creating a memory leak. Since there is no dynamic instantiation of modules (and hence axi fsms) we did not see this critical yet.

mslijepc commented 2 years ago

One proposal to fix it:

base.h

std::vector<std::unique_ptr<axi::fsm::fsm_handle>> allocated_ptrs;

base.cpp (need slight modification of axi::fsm::fsm_handle, since there is also potential memory leak)

auto* fsm_hndl = create_fsm_handle();
allocated_ptrs.emplace_back(fsm_hndl);
fsm_hndl->fsm = std::make_shared<AxiProtocolFsm>();