需要帮助查找内存泄漏
Need help finding memoryleak
我已经坐了好几个小时了,快把我逼疯了。我太新c++,所以如果除了内存泄漏以外的其他东西是错误的,我想知道!: D
问题是,如果我创建一个新的老师/助理/助教并打印出来,我得到:
{212} normal block at 0x005694B0, 8 bytes long.
Data: < V > BC 91 56 00 00 00 00 00
{210} normal block at 0x005691B8, 40 bytes long.
Data: < V Oliver > BC A4 16 00 B0 94 56 00 4F 6C 69 76 65 72 00 CD
{209} normal block at 0x00568F30, 8 bytes long.
Data: < V > 90 8E 56 00 00 00 00 00
{208} normal block at 0x00569468, 8 bytes long.
Data: <d V > 64 8E 56 00 00 00 00 00
{204} normal block at 0x00568E60, 76 bytes long.
Data: < h V elev > D4 A6 16 00 68 94 56 00 65 6C 65 76 00 CD CD CD
如果我一直重复它只是堆积,我会感激所有的帮助,我可以得到!: D
int main(){
int choise, count=0;
bool ta = false , teacher = false, assistant = false;
choise = meny();
Employee **work = NULL;
Employee **swapper = NULL;
Employment *tmp = NULL;
string name, types, subject, temp, type;
int birthyear, salary, workhours, points;
bool boss = false, ansvarig, key=false, cert;
while (choise != NULL){
if (choise > 0){
if (choise == 1){
fflush(stdin);
cout << endl << "Namn: ";
getline(cin, name);
cout << endl << "Fodelsear: ";
cin >> birthyear;
cin.ignore();
cout << "nTjänst: ";
getline(cin, type);
cout << "nLon: ";
cin >> salary;
cin.ignore();
cout << "nChef: ";
cin >> temp;
cin.ignore();
if (temp == "Ja" || temp == "ja"){
boss = true;
}
else
boss = false;
cout << "nTjanstgoringstid: ";
cin >> workhours;
cin.ignore();
cout << "nHuvudämne: ";
cin >> subject;
cin.ignore();
cout << "nProgramansvarig: ";
cin >> temp;
cout << "nnn";
cin.ignore();
if (temp == "Ja" || temp == "ja")
ansvarig = true;
else
ansvarig = false;
count++;
teacher = true;
}
else if (choise == 2){
fflush(stdin);
cout << endl << "Namn: ";
getline(cin, name);
cout << endl << "Fodelsear: ";
cin >> birthyear;
fflush(stdin);
cout << "nTjänst: ";
getline(cin, type);
cout << "nLon: ";
cin >> salary;
fflush(stdin);
cout << "nHuvudämne: ";
cin >> subject;
fflush(stdin);
cout << "Avklarade poang: ";
cin >> points;
fflush(stdin);
count++;
assistant = true;
}
else if (choise == 3){
fflush(stdin);
cout << endl << "Namn: ";
getline(cin, name);
cout << endl << "Fodelsear: ";
cin >> birthyear;
cin.ignore();
cout << "nTjänst: ";
getline(cin, type);
cout << "nLon: ";
cin >> salary;
cin.ignore();
cout << "nTjanstgoringstid: ";
cin >> workhours;
cin.ignore();
cout << "nAttestratt: ";
cin >> temp;
cin.ignore();
if (temp == "Ja" || temp == "ja"){
cert = true;
}
else
cert = false;
cout << "nHar huvudnyckel: ";
cin >> temp;
cin.ignore();
if (temp == "Ja" || temp == "ja"){
cert = true;
}
else
cert = false;
cout << "nnn";
count++;
ta = true;
}
else if (choise == 4){
for (int x = 0; x < (count); x++){
cout << work[x]->toString();
}
}
if (choise != 0 && choise!=4){
swapper = new Employee*[count];
if (teacher == true){
tmp = new Teacher(type, boss, salary, workhours, subject, ansvarig);
swapper[count - 1] = new Employee(name, birthyear, tmp);
teacher = false;
//delete tmp;
}
else if (ta == true){
tmp = new TA(type, boss, salary, workhours, cert, key);
swapper[count - 1] = new Employee(name, birthyear, tmp);
assistant = false;
// delete tmp;
}
else if (assistant == true){
tmp = new Assistant(type, salary, subject, points);
swapper[count - 1] = new Employee(name, birthyear, tmp);
// delete tmp;
}
for (int x = 0; x < count - 1; x++){
swapper[x] = work[x];
}
delete[] work;
work = swapper;
}
choise = meny();
}
}
/*for (int x = 0; x < count; x++){
delete work[x];
}*/
delete [] work;
_CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);
_CrtDumpMemoryLeaks();
return 0;
}
你从来没有delete
你的数组。你应该使用矢量
事情基本上是,每一个new
你做,你需要有一个delete
时,对象不再需要。这样做会释放内存。如果你不这样做,你就会永远积累大量的已分配内存。
只要动态分配内存而不释放内存,就会发生内存泄漏。这意味着对于出现在程序中的每个new
,必须存在相应的delete
,否则就会泄漏内存。
在你的代码中有很多delete
被注释掉了(例如,对于tmp
)。因此,我们马上就可以确定这些分配正在泄漏。
同样,你为swapper
分配的内存不会被释放(至少在第一次通过循环时)。此外,即使调用了delete [] work
,也不会对数组中的单个元素进行delete
处理,这些元素也是动态分配的。
并且由于所有这些分配都发生在循环中,因此您有这么多泄漏也就不足为奇了!
相关文章:
- 从构造函数抛出异常时如何克服内存泄漏
- malloc() 可能出现内存泄漏
- 这个极客对极客的trie实现是否存在内存泄漏问题
- 尽管遵循了规则,内存泄漏在哪里
- 为什么调用堆栈数组会导致内存泄漏
- 在简单示例中,Python3 + ctypes 回调会导致内存泄漏
- 使用模板类的自定义列表类型中的内存泄漏
- 为什么以下C++代码中存在内存泄漏?
- OpenCV 我应该使用智能指针来防止内存泄漏吗?
- 我是否生成线程并导致内存泄漏?
- 多线程程序中出现意外的内存泄漏
- 为什么此函数会导致内存泄漏?
- 在 C++ 库中使用cythonized python时内存泄漏
- 需要帮助查找内存泄漏
- 瓦尔格林德的内存泄漏使用新的
- 无法找出我的代码中的内存泄漏
- C++ 结构内部的unordered_map会导致内存泄漏问题吗?
- 可视化 使用 VS Code 查找C++应用程序中的内存泄漏
- Shared_ptr双链接列表内存泄漏
- C++ 在类中使用常量引用文本时 O2 内存泄漏