Bug 2372 - EnergySourceContainer assignment operator produces memory leaks
EnergySourceContainer assignment operator produces memory leaks
Status: CONFIRMED
Product: ns-3
Classification: Unclassified
Component: energy
ns-3-dev
All All
: P5 critical
Assigned To: Cristiano Tapparello
:
Depends on:
Blocks:
  Show dependency treegraph
 
Reported: 2016-04-10 17:49 EDT by Tommaso Pecorella
Modified: 2016-04-10 17:49 EDT (History)
1 user (show)

See Also:


Attachments
test script (400 bytes, text/x-csrc)
2016-04-10 17:49 EDT, Tommaso Pecorella
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tommaso Pecorella 2016-04-10 17:49:19 EDT
Created attachment 2383 [details]
test script

The problem was first spotted in the ns-3-users group here:
https://groups.google.com/forum/#!starred/ns-3-users/751tJTYfcv0

Alexander Krotov found the problem and described in the ns-dev mailing list.
Attached there's the minimal script to reproduce the bug.

Quoting Alexander:
> I have minimized the example down to attached one.
>
> Under valgrind, it shows memory errors.
> If I replace the lines
>  EnergySourceContainer energySources;
>  energySources = basicSourceHelper.Install (clientNodes);
> with
>  EnergySourceContainer energySources = basicSourceHelper.Install (clientNodes);
> then there are no errors.
>
> Now, I think the problem is that EnergySourceContainer (unlike
> NodeContainer, for example) is derived from Object, so it must be created
> with CreateObject. You can see how to do it correctly in
> src/energy/examples/li-ion-energy-source.cc .
> 
> The main problem is that EnergySourceHelper::Install method, which is
> inherited by BasicEnergySourceHelper, returns EnergySourceContainer,
> instead of Ptr<EnergySourceContainer>. It is not the correct way to pass
> Objects around.
> 
> I think the solution is to make EnergySourceContainer, like
> NetDeviceContainer, not derived from Object.

I totally agree with Alexander. Containers should have a constant use patterns, and having one behaving differently leads to hard-to-find mistakes.