How to avoid this memory leak?

Hello Forum!

I have a class SceneNode that uses a vector of std::unique_ptr<SceneNode>'s to store it's children and a SceneNode* to it's parent.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#ifndef SCENE_NODE_H
#define SCENE_NODE_H

#include <memory>
#include <SFML\Graphics.hpp>

class SceneNode: public sf::Drawable, public sf::Transformable, private sf::NonCopyable {

public:

	SceneNode();

	void										update(float delta);

	void										attachChild(std::unique_ptr<SceneNode> child);
	std::unique_ptr<SceneNode>					detachChild(const SceneNode& node);

	sf::Vector2f								getWorldPosition() const;
	sf::Transform								getWorldTransform() const;

private:

	std::vector<std::unique_ptr<SceneNode>>		children;
	SceneNode*									parent;

	virtual void								updateCurrent(float delta);
	void										updateChildren(float delta);

	virtual void								draw(sf::RenderTarget& target, sf::RenderStates states) const;
	virtual void								drawCurrent(sf::RenderTarget& target, sf::RenderStates states) const;
	void										drawChildren(sf::RenderTarget& target, sf::RenderStates states) const;

};

#endif 


I have Visual Leak Detector enabled and get this error:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
WARNING: Visual Leak Detector detected memory leaks!
  Leak Hash: 0x8A0A1B5B, Count: 1, Total 8 bytes
  Call Stack (TID 1988):
    0x003EC260 (File and line number not available): MSVCR120D.dll!operator new
    d:\programme\microsoft visual studio 12.0\vc\include\xmemory0 (848): Endless World.exe!std::_Wrap_alloc<std::allocator<std::_Container_proxy> >::allocate
    d:\programme\microsoft visual studio 12.0\vc\include\vector (624): Endless World.exe!std::_Vector_alloc<0,std::_Vec_base_types<std::unique_ptr<SceneNode,std::default_delete<SceneNode> >,std::allocator<std::unique_ptr<SceneNode,std::default_delete<SceneNode> > > > >::_Alloc_proxy + 0xA bytes
    d:\programme\microsoft visual studio 12.0\vc\include\vector (604): Endless World.exe!std::_Vector_alloc<0,std::_Vec_base_types<std::unique_ptr<SceneNode,std::default_delete<SceneNode> >,std::allocator<std::unique_ptr<SceneNode,std::default_delete<SceneNode> > > > >::_Vector_alloc<0,std::_Vec_base_types<std::unique_ptr<SceneNode,std::defau
    d:\programme\microsoft visual studio 12.0\vc\include\vector (681): Endless World.exe!std::vector<std::unique_ptr<SceneNode,std::default_delete<SceneNode> >,std::allocator<std::unique_ptr<SceneNode,std::default_delete<SceneNode> > > >::vector<std::unique_ptr<SceneNode,std::default_delete<SceneNode> >,std::allocator<std::unique_ptr<SceneNod
    d:\max\eigene dokumente\visual studio 2013\projects\endless world\endless world\scenenode.cpp (9): Endless World.exe!SceneNode::SceneNode + 0xAD bytes
    d:\max\eigene dokumente\visual studio 2013\projects\endless world\endless world\gamescene.cpp (7): Endless World.exe!GameScene::GameScene
    d:\max\eigene dokumente\visual studio 2013\projects\endless world\endless world\application.cpp (14): Endless World.exe!Application::Application + 0x3F bytes
    d:\max\eigene dokumente\visual studio 2013\projects\endless world\endless world\main.cpp (9): Endless World.exe!main + 0x1B bytes
    f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (626): Endless World.exe!__tmainCRTStartup + 0x19 bytes
    f:\dd\vctools\crt\crtw32\dllstuff\crtexe.c (466): Endless World.exe!mainCRTStartup
    0x7514338A (File and line number not available): kernel32.dll!BaseThreadInitThunk + 0x12 bytes
    0x77599F72 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x63 bytes
    0x77599F45 (File and line number not available): ntdll.dll!RtlInitializeExceptionChain + 0x36 bytes
  Data:
    D0 61 67 06    00 00 00 00                                   .ag..... ........


Visual Leak Detector detected 1 memory leak (44 bytes).
Largest number used: 1672 bytes.
Total allocations: 1804 bytes.
Visual Leak Detector is now exiting.


It's the line of the initialization list of SceneNode.

1
2
3
4
5
SceneNode::SceneNode() : 
children(),
parent(nullptr) {

}


Edit: Code format wrong!
Last edited on
How do you call attachChild(...) and detachChild(...)?

This shouldn't compile. unique_ptr is supposed to be not copyable
The implementation of attachChild(...):

1
2
3
4
void SceneNode::attachChild(std::unique_ptr<SceneNode> child) {
	child->parent = this;
	children.push_back(std::move(child));
}


And then I attach a child like this:

1
2
std::unique_ptr<Player> playerPtr(new Player());
sceneGraph.attachChild(std::move(playerPtr));


But that isn't the problem. I don't know how to handle the raw pointer to the parent.
Topic archived. No new replies allowed.