Bug 2372

Summary: EnergySourceContainer assignment operator produces memory leaks
Product: ns-3 Reporter: Tommaso Pecorella <tommaso.pecorella>
Component: energyAssignee: Cristiano Tapparello <cristiano.tapparello>
Status: CONFIRMED ---    
Severity: critical CC: ns-bugs
Priority: P5    
Version: ns-3-dev   
Hardware: All   
OS: All   
Attachments: test script

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.