[英]Why do I have a memory leak in my c++ code?
我是 c++ 的新手,我有可以編譯但不會發布到 linux 的代碼,因為它說我在錯誤中有 memory 泄漏。 請幫我找出代碼中的錯誤。 Linux 使用 valgrind,它發現了泄漏。 請幫我找出錯誤並修復它。
Output 與 memory 泄漏:
==6858== Memcheck, a memory error detector
==6858== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==6858== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==6858== Command: ws
==6858==
Enter the following Data:
----------------------
lukE
sky
fett
feT
Jack
!
----------------------
Star Wars phone direcotry search
-------------------------------------------------------
Enter a partial name to search (no spaces) or enter '!' to exit
> lukE
Luke Skywalker: (301) 555-0630
Enter a partial name to search (no spaces) or enter '!' to exit
> sky
Luke Skywalker: (301) 555-0630
Enter a partial name to search (no spaces) or enter '!' to exit
> fett
Jango Fett: (905) 555-6016
Boba Fett: (905) 555-9382
Enter a partial name to search (no spaces) or enter '!' to exit
> feT
Jango Fett: (905) 555-6016
Boba Fett: (905) 555-9382
Enter a partial name to search (no spaces) or enter '!' to exit
> Jack
Enter a partial name to search (no spaces) or enter '!' to exit
> !
Thank you for using Star Wars directory.
----------------------------------
Broken Phone Book phone direcotry search
-------------------------------------------------------
badDataFile.txt file not found!
Thank you for using Broken Phone Book directory.
==6858==
==6858== HEAP SUMMARY:
==6858== in use at exit: 568 bytes in 1 blocks
==6858== total heap usage: 384 allocs, 383 frees, 88,684 bytes allocated
==6858==
==6858== 568 bytes in 1 blocks are still reachable in loss record 1 of 1
==6858== at 0x4C29F73: malloc (vg_replace_malloc.c:309)
==6858== by 0x578CC4C: __fopen_internal (in /usr/lib64/libc-2.17.so)
==6858== by 0x40114B: sdds::phoneDir(char const*, char const*) (in /home/kshiman/OOP244/DIY/ws)
==6858== by 0x40156E: main (in /home/kshiman/OOP244/DIY/ws)
==6858==
==6858== LEAK SUMMARY:
==6858== definitely lost: 0 bytes in 0 blocks
==6858== indirectly lost: 0 bytes in 0 blocks
==6858== possibly lost: 0 bytes in 0 blocks
==6858== still reachable: 568 bytes in 1 blocks
==6858== suppressed: 0 bytes in 0 blocks
==6858==
==6858== For lists of detected and suppressed errors, rerun with: -s
==6858== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
這是 phone.cpp 文件:
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <stdio.h>
#include "cStrTools.h"
#include "Phone.h"
using namespace std;
namespace sdds {
int n;
record list;
void programtitle(const char* programTitle) {
cout << programTitle << " phone direcotry search\n";
cout << "-------------------------------------------------------\n";
}
void phoneDir(const char* programTitle, const char* fileName) {
programtitle(programTitle);
while (1) {
FILE* fptr = fopen(fileName, "r");
if (!fptr) {
cout << fileName << " file not found!\n";
break;
}
else {
cout << "Enter a partial name to search (no spaces) or enter '!' to exit\n> ";
string Name;
cin >> Name;
if (Name == "!")
break;
while (1) {
fscanf(fptr, "%[^\t]s", list.name); getc(fptr);
fscanf(fptr, "%d", &list.area);getc(fptr);
fscanf(fptr, "%d", &list.prefix); getc(fptr);
fscanf(fptr, "%d", &list.number);
if (getc(fptr) == EOF)
break;
if (have(list.name, Name)) {
if (list.number > 1000) {
cout << list.name << ": (" << list.area << ") " << list.prefix << "-" << list.number << "\n";
}
else {
cout << list.name << ": (" << list.area << ") " << list.prefix << "-" << "0" << list.number << "\n";
}
}
}
}
fclose(fptr);
}
cout << "Thank you for using " << programTitle << " directory.\n";
}
}
這是結構:
struct record {
char name[50];
int prefix;
int area;
int number;
};
這是主文件:
#include "Phone.h"
using namespace std;
using namespace sdds;
#define _CRT_SECURE_NO_WARNINGS
int main() {
phoneDir("Star Wars", "phones.txt");
return 0;
}
這里,
if (Name == "!")
break;
您正在打破外循環,因此在不關閉文件的情況下退出代碼。 這將泄漏 memory。 您應該在break
之前關閉文件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.