简体   繁体   中英

Why do I have a memory leak in my c++ code?

I'm new to c++ and I have code that compiles but won't publish to linux because it says I have a memory leak in the error. Please help me find the error in the code. Linux uses valgrind, which finds the leak. Please help me find the error and fix it.

Output with memory leak:

==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)

this is phone.cpp file:

#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";

    }
    
}

this is structure:

    struct record {
        char name[50];
        int prefix;
        int area;
        int number;
    };

this is the main file:

#include "Phone.h"
using namespace std;
using namespace sdds;
#define _CRT_SECURE_NO_WARNINGS
int main() {
    phoneDir("Star Wars", "phones.txt");
    return 0;
}

Here,

if (Name == "!")
    break;

you are breaking the outter loop, hence exiting the code without closing the file. This will leak memory. You should close the file before the break .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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