简体   繁体   English

C++ 代码中的内存泄漏

[英]Memory leak in c++ code

i'm writting a code in Visual Studio using c++.我正在使用 C++ 在 Visual Studio 中编写代码。 My test say that i have memory leaks.我的测试说我有内存泄漏。 I dont understand why.我不明白为什么。

here is the error:这是错误:

Detected memory leaks!检测到内存泄漏! Dumping objects -> e:\\gbm\\inf1005c\\td6\\exercice1\\exercice1.cpp(175) : {1417} normal block at 0x0073C7C8, 0 bytes long.转储对象 -> e:\\gbm\\inf1005c\\td6\\exercice1\\exercice1.cpp(175) : {1417} 正常块在 0x0073C7C8,0 字节长。 Data: <>数据:<>

thanks a lot.多谢。

please note that colletion is a struct and that the line with the memory leak is this one: nouvelleCollection.livres = new Livre*[nTitresPareils];请注意 colletion 是一个结构体,内存泄漏的那一行是这样的: nouvelleCollection.livres = new Livre*[nTitresPareils];

Collection retrouverLivresParNom(const wstring& titre, const Collection& collection) // Mettre les bons paramètres.

{ int nTitresPareils = 0; { int nTitresPareils = 0;

// Retrouver les livres dans la collection dont le titre correspond à la recherche
for (int i = 0; i < collection.nLivres; i++){
    bool trouve = (wcsstr(collection.livres[i]->titre, titre.c_str()) != nullptr);
    if (trouve)
        nTitresPareils ++;
}

// Allouer l'espace qui contiendra le tableau des livres trouvés
Collection nouvelleCollection;
nouvelleCollection.livres = new Livre*[nTitresPareils]; 

// Copier les pointeurs vers les livres trouvés
int compteur = 0;

for (int i = 0; i < collection.nLivres; i++){
    bool trouve = (wcsstr(collection.livres[i]->titre, titre.c_str()) != nullptr);
    if (trouve){
        nouvelleCollection.livres[compteur] = collection.livres[i];
        compteur++;
    }
}

nouvelleCollection.nLivres = nTitresPareils;
nouvelleCollection.nLivresAlloues = nTitresPareils;

// Retourner le nombre de livres trouvés
return nouvelleCollection;

} }

First you need to make sure that the destructor of Collection ( Collection::~Collection() ) calls delete[] on the livres member.首先,您需要确保 Collection 的析构函数 ( Collection::~Collection() ) 在livres成员上调用delete[] This will prevent your memory leak.这将防止您的内存泄漏。 Even structs can have destructors.甚至结构体也可以有析构函数。

However, when you do that, you also have to make another fix.但是,当您这样做时,您还必须进行另一次修复。 The default copy constructor will do a shallow copy of the members of that class, so the pointer to the array of books will be copied but not the whole array.默认的复制构造函数将对该类的成员进行浅拷贝,因此将复制指向书籍数组的指针,但不会复制整个数组。

Because you're returning the Collection by value, then the local copy of the Collection will be destroyed upon return.因为您是按值返回 Collection,所以 Collection 的本地副本将在返回时销毁。 When that happens the livres array will be deallocated and the Collection variable to which the return value is assigned in the calling function will have a null livres pointer or worse a dangling pointer.当发生这种情况时, livres数组将被释放,并且在调用函数中分配返回值的 Collection 变量将具有空的livres指针或更糟糕的悬空指针。

So you also need to do one of two things:所以你还需要做两件事之一:

1) You need to create your Collection on the heap using new Collection and return it by pointer ( Collection* ) 1) 您需要使用new Collection在堆上创建new Collection并通过指针 ( Collection* ) 返回它

OR或者

2) You need to create a copy constructor in Collection to do a deep copy of the list of books ( livres ). 2)您需要在 Collection 中创建一个复制构造函数来对书籍列表( livres )进行深度复制。

The comments on your question are good too, consider using a std::vector instead of allocating your own array.关于您的问题的评论也很好,请考虑使用 std::vector 而不是分配您自己的数组。 If you use a std::vector, you don't have to worry about specially handling its destruction or copying.如果您使用 std::vector,则不必担心专门处理其销毁或复制。

Bonne Chance!博恩机会!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM