qt - Memory leak in C++ function using std::swap -


i'm using qt creator , i'm having trouble memory leak. i've read posts dynamic memory allocation i've seen, can't understand why function accumulating in memory.

i'm sure i've pinpointed function causes problem:

void csimwindow::clonenet(int origin, int destination)      int newnumsensors = netvector[origin].getnumsensors();     int newnumactuators = netvector[origin].getnumactuators();     int newnumneurons = netvector[origin].getnumneurons();      cnet newnet(newnumneurons, 0);     newnet.setnumsensors(newnumsensors);     newnet.setnumactuators(newnumactuators);      (int = 0; < netvector[origin].getnumneurons(); i++)     {         ...     }     std::swap(newnet, netvector[destination]);  } 

i'm quite newbie, understand it, objects created inside function should destroyed when it's finished. if can tell me why function causes memory leak, thank in advance.

the way see there 3 possibilities:

1: (the likely) cnet destructor not de-allocate memory reserved constructor.

to check that, use global cnet newnet variable, , not re-create temporary variables every time go routine (rather set values of global newnet variable), not keep calling constructor/destructor.

2: std::swap(newnet, netvector[destination]); call, think creates temporary variable explained here:

http://www.cplusplus.com/reference/algorithm/swap/

try comment std::swap call , see happens.

3: inside loop fishy, not provide details there.

good luck.


Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -