mercredi 16 novembre 2022

Anyone can help me to free memory leak in this code?

This is the code it occupies 4 alloc & free only 3, Anyone can help me to do Its urgent

/* Citation and Sources...
Final Project Milestone 2
Module: Menu
Filename: Parking.cpp
Version 1.0
Author: Swati Gupta
Revision History
-----------------------------------------------------------
Date Reason
2022/11/13 Preliminary release
-----------------------------------------------------------
I have done all the coding by myself and only copied the code 
that my professor provided to complete my project milestones.
-----------------------------------------------------------*/
#define _CRT_SECURE_NO_WARNINGS
#include <cstring>
#include <iostream>
#include <iomanip>
#include <fstream>
#include "Parking.h"
#include "Menu.h"

using namespace std;
namespace sdds {
    Parking::Parking(const char* datafile) :m_datafile(nullptr), m_parkingMenu("Parking Menu, select an action:"), m_vehicleMenu("Select type of the vehicle:", 1) {
        if (datafile != nullptr && datafile[0] != '\0') { 
            m_datafile = new char[strlen(datafile) + 1];
            strcpy(m_datafile, datafile);
            if (load()) {
                m_parkingMenu.add("Park Vehicle");
                m_parkingMenu.add("Return Vehicle");
                m_parkingMenu.add("List Parked Vehicles");
                m_parkingMenu.add("Find Vehicle");
                m_parkingMenu.add("Close Parking (End of day)");
                m_parkingMenu.add("Exit Program");
                m_vehicleMenu.add("Car");
                m_vehicleMenu.add("Motorcycle");
                m_vehicleMenu.add("Cancel");
                
            } 
        }
        else 
        { 
            cout << "Error in data file" << endl;
            m_datafile = nullptr;
        }
    }
    void Parking::parkVehicle() {
        m_vehicleMenu.display();
        int option = 0;
        cin >> option;
        if (option == 1) { 
            cout << "---------------------------------" << endl;
            cout << "Parking Car" << endl;
            cout << "---------------------------------" << endl<<endl;
        } 
        else if (option == 2) { 
            cout << "---------------------------------" << endl;
            cout << "Parking Motorcycle" << endl;
            cout << "---------------------------------" << endl<<endl;
        } 
        else if (option == 3) {
            cout << "---------------------------------" << endl;
            cout << "Cancelled parking" << endl;
            cout << "---------------------------------" << endl<<endl;
        } 
    }
    void Parking::returnVehicle() { 
        cout << "---------------------------------" << endl;
        cout << "Returning Vehicle" << endl;
        cout << "---------------------------------" << endl<< endl;
    }
    void Parking::listParkedVheicles() { 
        cout << "---------------------------------" << endl;
        cout << "Listing Parked Vehicles" << endl;
        cout << "---------------------------------" << endl<< endl;
    }
    void Parking::FindVehicle() {
        cout << "---------------------------------" << endl;
        cout << "Finding a Vehicle" << endl;
        cout << "---------------------------------" << endl<< endl;
    }
    bool Parking::closeParking() {
        bool check = false;
        char choice;
        char* option = nullptr;
        cout << "This will close the parking; All the vehicles will be removed!" << endl;
        cout << "Are you sure? (Y)es/(N)o: ";
        option = new char[7];
        do {
            cin >> option;
            if ((option[0] != 'Y' && option[0] != 'y' && option[0] != 'N' && option[0] != 'n') || strlen(option) > 1)
            { 
                cout << "Invalid response, only (Y)es or (N)o are acceptable, retry: ";
            }
            else 
            { 
                check = true; 
            }
            cin.clear();
            cin.ignore(1000, '\n');
        }
        while (!check);
        cout << "Ending application!" << endl;
        choice = tolower(option[0]);
        delete[] option;
        option = nullptr;
        return choice == 'y';
    }
    bool Parking::exitParkingApp() {
        bool check = false;
        cout << "This will terminate the application and save the data!" << endl;
        cout << "Are you sure? (Y)es/(N)o: ";
        char ori;
        do {
            cin >> ori;
            if ((ori == 'Y') || (ori == 'y'))
            { 
                check = true; 
            }
            else if ((ori == 'N')||(ori == 'n'))
            {
              run();
            }
            else
            { 
                cout << "Invalid response, only (Y)es or (N)o are acceptable, retry: ";
            }
            cin.clear();
            cin.ignore(1000, '\n');
        }
        while (!check);
        cout << "Exiting application!" << endl;
        cout <<"---------------------------------\n";
        cout<<"Saving data into ParkingData.csv\n";
        cout<<"---------------------------------\n" << endl;
        exit(1);
        return ori == 'y';
        
        
    }
    void Parking::parkingStatus() const { 
        cout << "****** Valet Parking ******" << endl;
    }
    Parking::~Parking() { 
        save();
        setEmpty(); 
    }
    bool Parking::load() {
        if (!isEmpty())
        cout<<"---------------------------------\n";
        cout << "loading data from " << m_datafile << endl;
        cout<<"---------------------------------\n\n";
        return (!isEmpty());
    }
    void Parking::save() {
        if (!isEmpty()) { 
            cout <<"---------------------------------\n";
            cout<<"Saving data into ParkingData.csv\n";
            cout<<"----------------------------------";
        }
    }
    bool Parking::isEmpty() const { 
        return m_datafile == nullptr;
    }
    void Parking::setEmpty() { 
        delete[] m_datafile;m_datafile = nullptr; 
    }
    int Parking::run() {
        bool done = false;
        int selection = 0;
        while (!isEmpty() && !done) {
            parkingStatus();
            m_parkingMenu.display();
            cin >> selection;
            if (selection == 1) { 
                parkVehicle(); 
            }
            else if (selection == 2) { 
                returnVehicle(); 
            }
            else if (selection == 3) {
                listParkedVheicles();
            }
            else if (selection == 4) {
                FindVehicle();
            }
            else if (selection == 5) { 
                done = closeParking();
            }
            else if (selection == 6) { 
                done = exitParkingApp();
            }
        }
        if (m_datafile == nullptr) {
            return 1;
        }
        else 
        {
            return 0;
        }
    }
}

It is aparking system where we can use some random option coz of dummy code

run your program as follows to check for possible memory leaks (assuming your executable name is ms):

valgrind --show-error-list=yes --leak-check=full --show-leak-kinds=all --track-origins=yes ms --show-error-list=yes: show the list of detected errors --leak-check=full: check for all types of memory problems --show-leak-kinds=all: show all types of memory leaks identified (enabled by the previous flag) --track-origins=yes: tracks the origin of uninitialized values (g++ must use -g flag for compilation, so the information displayed here is meaningful). To check the output, use a program that can compare text files. Search online for such a program for your platform, or use diff available on Just free memory block, for avoid the memory leak. thanks

Aucun commentaire:

Enregistrer un commentaire