Bonjour à tous,

Je viens vous voir, vous les experts (enfin j'espère ) pour palier à mon problème de fuite mémoire.

Je travaille sur un petit programme de gestion de "forme".

J'ai donc 4 quatres classes.

Cercle - permettant d'instancier un Cercle avec son rayon et centre
Polygone - permettant d'instancier un Polygone par ces points contenu dans deux vector<double> x et y.

Forme, classe abstraite et parente de Cercle et Polygone

Usine permettant de stocker des formes (cercle, polygone) par le biais d'une map.


Mon main me permet d'instancier un cercle ou un polygone et de mettre une copie de celui-ci dans l'usine.

Pour un cercle, j'ai aucun soucis. Pour un polygone voici le code suivi :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
 
string nomPolygone;
int nombrePoints;
vector<double> xPolygone, yPolygone;
double xTmp, yTmp;
Forme* tmp;
 
cout << "Identifiant de votre Polygone ?" << endl;	
cin >> nomPolygone;
cout << "" << endl;
cout << "Combien de points contiendra votre polygone ?" << endl;	
cin >> nombrePoints;
cout << "" << endl;
 
for(i=0;i<nombrePoints;i++)
{
	cout << "Enregistrement N " << i+1 << " :" << endl;
	cout << "--------------------" << endl;
 
	cout << "X = ";
	cin >> xTmp;						
	cout << "Y = ";
	cin >> yTmp;
	xPolygone.push_back(xTmp);
	yPolygone.push_back(yTmp);
}
tmp = new Polygone(xPolygone, yPolygone);
test = U.ajouterForme(nomPolygone,tmp);
 
if(test) cout << "Ajout réussi" << endl;
else cout << "Ajout non réussi - la clé existe déjà" << endl;
 
delete tmp;
On va donc dans ajouterForme (const string&, Forme*);

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 
bool Usine::ajouterForme(const string &_identifiant, Forme* obj)
{
	//Insertion de l'identifiant et de l'objet dans une pair et ensuite dans le catalogue
	//Le pointeur de Forme ajouté dans l'usine correspond à une copie de celui passé en parametre
	if(obj==0) 
	{
		return 0;
	}	
	else if(!catalogue.count(_identifiant)) 
	{ 
		catalogue.insert(pair<string, Forme*>(_identifiant,obj->dupliquer())); 
		return 1; 
	}
	else
	{
		return 0;
	}
}
On appelle donc dupliquer() (de polygone par polymorphisme)

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
 
Forme* Polygone::dupliquer() const
{
	return new Polygone(*this);
}
qui appelle le constructeur de recopie de polygone

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
 
Polygone::Polygone(const Polygone& obj)
{
	x = obj.x;
	y = obj.y;
	cout << "CREATION D'UN POLYGONE PAR RECOPIE" << endl;
}
Et la dans le constructeur de recopie, c'est le drame ! En testant j'ai vu que si je commentais les deux lignes de code sur les vecteurs, j'avais aucune fuite, avec ...

valgrind me donne en rapport :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
 
==2513== 
==2513== HEAP SUMMARY:
==2513==     in use at exit: 192 bytes in 6 blocks
==2513==   total heap usage: 34 allocs, 28 frees, 706 bytes allocated
==2513== 
==2513== 32 bytes in 1 blocks are definitely lost in loss record 1 of 6
==2513==    at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==2513==    by 0x805418F: __gnu_cxx::new_allocator<double>::allocate(unsigned int, void const*) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8053DF9: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805707A: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned int, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056E3F: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x80566C7: Polygone::Polygone(std::vector<double, std::allocator<double> >, std::vector<double, std::allocator<double> >) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8050D13: main (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513== 
==2513== 32 bytes in 1 blocks are definitely lost in loss record 2 of 6
==2513==    at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==2513==    by 0x805418F: __gnu_cxx::new_allocator<double>::allocate(unsigned int, void const*) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8053DF9: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805707A: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned int, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056E3F: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x80566DC: Polygone::Polygone(std::vector<double, std::allocator<double> >, std::vector<double, std::allocator<double> >) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8050D13: main (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513== 
==2513== 32 bytes in 1 blocks are definitely lost in loss record 3 of 6
==2513==    at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==2513==    by 0x805418F: __gnu_cxx::new_allocator<double>::allocate(unsigned int, void const*) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8053DF9: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805707A: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned int, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056E3F: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056784: Polygone::Polygone(Polygone const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056D1A: Polygone::dupliquer() const (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8054A9C: Usine::ajouterForme(std::string const&, Forme*) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8050DAB: main (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513== 
==2513== 32 bytes in 1 blocks are definitely lost in loss record 4 of 6
==2513==    at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==2513==    by 0x805418F: __gnu_cxx::new_allocator<double>::allocate(unsigned int, void const*) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8053DF9: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805707A: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned int, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056E3F: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805679C: Polygone::Polygone(Polygone const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056D1A: Polygone::dupliquer() const (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8054A9C: Usine::ajouterForme(std::string const&, Forme*) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8050DAB: main (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513== 
==2513== 32 bytes in 1 blocks are definitely lost in loss record 5 of 6
==2513==    at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==2513==    by 0x805418F: __gnu_cxx::new_allocator<double>::allocate(unsigned int, void const*) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8053DF9: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805707A: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned int, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056E3F: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056784: Polygone::Polygone(Polygone const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056D1A: Polygone::dupliquer() const (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8054C11: Usine::creerForme(std::string const&) const (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805100D: main (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513== 
==2513== 32 bytes in 1 blocks are definitely lost in loss record 6 of 6
==2513==    at 0x402641D: operator new(unsigned int) (vg_replace_malloc.c:255)
==2513==    by 0x805418F: __gnu_cxx::new_allocator<double>::allocate(unsigned int, void const*) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8053DF9: std::_Vector_base<double, std::allocator<double> >::_M_allocate(unsigned int) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805707A: double* std::vector<double, std::allocator<double> >::_M_allocate_and_copy<__gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > > >(unsigned int, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >, __gnu_cxx::__normal_iterator<double const*, std::vector<double, std::allocator<double> > >) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056E3F: std::vector<double, std::allocator<double> >::operator=(std::vector<double, std::allocator<double> > const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805679C: Polygone::Polygone(Polygone const&) (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8056D1A: Polygone::dupliquer() const (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x8054C11: Usine::creerForme(std::string const&) const (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513==    by 0x805100D: main (in /home/jonathan/TPC++/TP6/V1.1/Formes)
==2513== 
==2513== LEAK SUMMARY:
==2513==    definitely lost: 192 bytes in 6 blocks
==2513==    indirectly lost: 0 bytes in 0 blocks
==2513==      possibly lost: 0 bytes in 0 blocks
==2513==    still reachable: 0 bytes in 0 blocks
==2513==         suppressed: 0 bytes in 0 blocks
==2513== 
==2513== For counts of detected and suppressed errors, rerun with: -v
==2513== ERROR SUMMARY: 6 errors from 6 contexts (suppressed: 17 from 6)
Une idée ? (ou plus ...)