简体   繁体   中英

Memory Heap and Leak Summary on Valgrind

Our school submissions are all through matrix using valgrind, which checks the output line by line. However, when submitting, I'm getting "Memory Error Detected".

Compile result:

Success! No errors or warnings...

Execution:

Script started, file is student_output.txt
Script started, file is student_output.txt
==110143== Memcheck, a memory error detector
==110143== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==110143== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==110143== Command: ms
==110143==
Loading Data
Library Application:

When inputing data:

Publication Title: e
==111103== Conditional jump or move depends on uninitialised value(s)
==111103==    at 0x404318: sdds::LibApp::search(int, char) (LibApp.cpp:145)
==111103==    by 0x404ABC: sdds::LibApp::removePublication() (LibApp.cpp:329)
==111103==    by 0x404DC1: sdds::LibApp::run() (LibApp.cpp:415)
==111103==    by 0x404F6F: runLibApp(char const*) (LibAppMain_prof.cpp:9)
==111103==    by 0x405084: main (LibAppMain_prof.cpp:20)
==111103==

Other information

I also get error while adding a publication, (using the newPublication() function), which in the same file below.

==140314== Use of uninitialised value of size 8
==140314==    at 0x404926: sdds::LibApp::newPublication() (LibApp.cpp:282)
==140314==    by 0x404DAA: sdds::LibApp::run() (LibApp.cpp:411)
==140314==    by 0x404F6F: runLibApp(char const*) (LibAppMain_prof.cpp:9)
==140314==    by 0x405084: main (LibAppMain_prof.cpp:20)
==140314==
==140314== Use of uninitialised value of size 8
==140314==    at 0x40493F: sdds::LibApp::newPublication() (LibApp.cpp:284)
==140314==    by 0x404DAA: sdds::LibApp::run() (LibApp.cpp:411)
==140314==    by 0x404F6F: runLibApp(char const*) (LibAppMain_prof.cpp:9)
==140314==    by 0x405084: main (LibAppMain_prof.cpp:20)
==140314==

The file is posted below.

LibApp.cpp

#define _CRT_SECURE_NO_WARNINGS
#include <fstream>
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include "LibApp.h"
#include "Book.h"
#include "PublicationSelector.h"

namespace sdds {

int NOLP=0;
int LLRN=0;

      bool LibApp::confirm(const char* message)
      {
         Menu conf(message);
         conf<<"Yes";  
         int t_return = conf.run();

         if(t_return) return true;

          return false;

      }

      LibApp::LibApp(const char filename[256])
      {  

         
         m_mainMenu << "Add New Publication" 
         << "Remove Publication" 
         << "Checkout publication from library"
         <<"Return publication to library";
         m_exitMenu << "Save changes and exit" << "Cancel and go back to the main menu";

         strcpy(m_filename, filename);
        

         m_publicationMenu << "Book" << "Publication" ;

         load();
      }

      LibApp::~LibApp()
      {

         for (int i = 0; i< NOLP ; i++) {
            delete PPA[i];
         }
      }

      void LibApp::load(){

   
         std::cout<<"Loading Data\n";
         
         std::ifstream infile(m_filename);
         char type{};
         
         for (int i = 0; infile ; i++) {
            infile >> type;
            infile.ignore();


            
            if (infile) {
            
               if (type == 'P')
                  PPA[i] = new Publication;
               else if (type == 'B')
                  PPA[i] = new Book;
               else std::cout<<"no data\n";

               if (PPA[i] && i < SDDS_LIBRARY_CAPACITY ) {

                  infile >> *PPA[i];

                  LLRN=PPA[i]->getRef();

                  NOLP++;



               }

            
            }
         }
      
      }  
      
      
      
      void LibApp::save(){
         std::cout<<"Saving Data\n";

    
         std::ofstream outfile(m_filename);
       
         
         for (int i = 0; i < NOLP; i++) {
            
            if (PPA[i]->getRef()!=0) {
            
               outfile << *PPA[i] << std::endl;

               
            
            }
         }      
         

      
      }   

      
      void prnPub(Publication* p[], int size, int ref) {
         int i;
         for (i = 0; i < size; i++) {
            if (ref == p[i]->getRef()) {
               std::cout << *p[i] << std::endl;
               i = size; 
            }
         }
      }

      int LibApp::search(int option,char type){
         
       
         

         PublicationSelector ps("Select one of the following found matches:", 15);

         std::cout<<"Publication Title: ";

         char title[256];

         std::cin.getline(title,256);


         if(option==1)
         {
            for (int i = 0; i< NOLP; i++) {
                     
                     if (strstr(*PPA[i],title) && PPA[i]->getRef()!=0 && type==PPA[i]->type())
                        ps << PPA[i]; 
            }
         }
         else if(option==2)
         {
            for (int i = 0; i< NOLP; i++) {
                     
                     if (strstr(*PPA[i],title) && !PPA[i]->onLoan() && PPA[i]->getRef()!=0 && type==PPA[i]->type())
                        ps << PPA[i]; 
            }
         }
         else if(option==3)
         {
            for (int i = 0; i< NOLP; i++) {
                     
                     if (strstr(*PPA[i],title) && PPA[i]->onLoan() && PPA[i]->getRef()!=0 && type==PPA[i]->type())
                        ps << PPA[i]; 
            }
         }


         int ref = 0;
  
         if (ps) {
            ps.sort(); 
            ref = ps.run(); 
            if (ref) {
               
               prnPub(PPA, NOLP , ref);
            }
            else {
               std::cout << "Aborted!\n";
            }
         }
         else {
            std::cout << "No matches to found!" << std::endl;
         }


         return ref;

   
      
      }   
      
      void LibApp::returnPub()  
      {
        
         std::cout<<"Return publication to the library\n";
         int i=m_publicationMenu.run();
         

         char type; 

         if(i==1) type='B';
         else type='P';

         int ref=search(3,type);

         
         if(ref!=0 && confirm("Returning publication?"))
         {
            Date date=getPub(ref)->checkoutDate();

            Date today;

            int days=today-date;
            days-=15;

           


            if(days>0)
            {
               
               std::cout << std::fixed;
               std::cout << std::setprecision(2);
               std::cout<<"Please pay $"<<float(days)*(0.5)<<" penalty for being "<<days<<" days late!\n";
            }

            getPub(ref)->set(0);
            std::cout<<"Publication returned\n";
            m_changed=true;

         }



      }

      void LibApp::newPublication()
      {  

         

         if( NOLP >= SDDS_LIBRARY_CAPACITY ) 
         {
            std::cout<<"Library is at its maximum capacity!\n";
            return;
         }

         std::cout<<"Adding new publication to library\n";

         int i=m_publicationMenu.run();

         Publication *p=nullptr;

         if(i==0)
         {  std::cout<<"Aborted!\n";
            return;
         }
         else if(i==1)
         {
            
            p = new Book;
            std::cin >> *p;
         }
         else if( i==2 )
         {
            p = new Publication;
            std::cin >> *p;
         }

         if(std::cin.fail())
         {
            std::cout<<"\nAborted!\n";
            exit(0);
         }


         if(confirm("Add this publication to library?"))
         {
            m_changed = true;

            PPA[NOLP]=p;

            LLRN=PPA[NOLP]->getRef();

            NOLP++;

            std::cout<<"Publication added\n";
         }

         if( !*p )
         {
            std::cout<<"Failed to add publication!\n";
            delete p;
         }

      }

      
      Publication* LibApp::getPub(int libRef)
      {
         for(int i=0;i<NOLP;i++)
         {
            if(libRef==PPA[i]->getRef()) return PPA[i];
         }

         return nullptr;

      }

      void LibApp::removePublication()
      {
         //std::cout<<;

         
            std::cout<<"Removing publication from the library\n";


         int i=m_publicationMenu.run();


         char type; 

         if(i==1) type='B';
         else 
         {  
            type='P';
         }
         int ref=search(1,type);

         
   
         if(ref!=0 && confirm("Remove this publication from the library?"))
         {
            m_changed = true;

            getPub(ref)->setRef(0);



            std::cout<<"Publication removed\n";
         }
         
      }

      void LibApp::checkOutPub()
      {  

         std::cout<<"Checkout publication from the library\n";
         int i=m_publicationMenu.run();
         char type; 

         if(i==1) type='B';
         else type='P';

         int ref=search(2,type);
         
         if(ref!=0 && confirm("Check out publication?"))
         {
            m_changed = true;

            int mn;

            std::cout << "Enter Membership Number: ";

            while (1)
            {              
               

               std::cin>>mn;

               if(mn > 9999 && mn <= 99999 ) break;

               std::cout<<"Invalid membership number, try again: ";
            }

            getPub(ref)->set(mn);

            std::cout<<"Publication checked out\n";
         }


      }



      LibApp::LibApp()
      {
         m_mainMenu << "Add New Publication" 
         << "Remove Publication" 
         << "Checkout publication from library"
         <<"Return publication to library";

         m_exitMenu << "Save changes and exit" << "Cancel and go back to the main menu";

         load();

      }


      

      void LibApp::run()
      {
         while(1)
         {  
            int option =  m_mainMenu.run();

            if( option == 1 )
            {
               newPublication();
            }
            else if( option == 2 ) 
            {
               removePublication();
            }
   

     else if( option == 3 )
        {
           checkOutPub();
        }
        else if( option == 4 )
        {
           returnPub();
        }
        else if( option == 0 )
        {
           if(m_changed)
           {
              
              int opn = m_exitMenu.run();

              if( opn == 1 )
              {
                 save();
                 break;
              }
              else if( opn == 2 )
              {
                 ;
              }
              else if( opn == 0)
              {  
                 if(confirm("This will discard all the changes are you sure?"))
                    break;
              }

           }
           else break;


        }

        std::cout<<std::endl;

        
     }



     std::cout<<std::endl;
     std::cout<<"-------------------------------------------\n";
     std::cout<<"Thanks for using Seneca Library Application\n";



      }

}

Please help me with this. My submission is today idk what the issues are.

Like the error message says:

Conditional jump or move depends on uninitialised value(s)
==111103==    at 0x404318: sdds::LibApp::search(int, char) (LibApp.cpp:145)

There is an if-statement (or something logically equivalent to an if-statement) at line 145 of LibApp.cpp that is basing its decision on which path to take on a variable that was never initialized to any value; as such, the behavior of that if-test is undefined (ie if could go either way depending on what arbitrary data happens to be present at that variable's memory location at the time).

So you'll need to look at the variable(s) present in the if-test at line 145 to figure out which ones aren't being set beforehand, and then fix the error by making sure they do get set first.

If necessary, you can add temporary if-tests just to provoke valgrind into giving you more results:

if (some_suspect_var != 0) fprintf(stderr, “Yea\n”);
else fprintf(stderr, “Nay\n”);

… then if you get a valgrind error on that if-line, you know that some_suspect_var is uninitialized at that point.

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