Search code examples
c++valgrind

C++ valgrind error message segmentation fault


I'm new to C++ and I'm trying to write an enigma machine simulator. I know I've done something funky with my pointers and I've run it through Valgrind, but I'm not sure what the error messages mean and where to begin fixing it? (What does suppressed mean in the leak summary?)

Here's part of the code where each component is created and where the error occurs.

Enigma::Enigma(int argc, char** argv){


errorCode = NO_ERROR;
plugboard = NULL;
*rotor = NULL;   //WHERE THE ERROR OCCURS
reflector = NULL;
rotorCount = 0;


//first check how many rotors there are
if (argc >= 5)
  rotorCount = argc - 4;
if (argc <= 4)       
  errorCode = INSUFFICIENT_NUMBER_OF_PARAMETERS;   

//pass files into each component and check if well-formed
if (errorCode == NO_ERROR){
  plugboard = new Plugboard(argv[1]);
  errorCode = plugboard -> errorCode;

if (errorCode == NO_ERROR){
  cout << "Plugboard configuration loaded successfully" << endl;
  reflector = new Reflector(argv[2]);
  errorCode = reflector -> errorCode;

  if (errorCode == NO_ERROR){
    cout << "Reflector configuration loaded successfully" << endl;
    rotor = new Rotor*[rotorCount];

    size_t i = 0;
    while (i < rotorCount && errorCode == NO_ERROR) {
      rotor[i] = new Rotor (argv[i+3]);
      i++;
      errorCode = rotor[i]-> errorCode;


      //destructor if rotor loading was unsuccessful 
      if (errorCode != NO_ERROR){
        for (int j=0; j<=i; j++)
          delete rotor[j];
        delete [] rotor;    

Here's the Valgrind error message:

 reflectors/I.rf rotors/I.rot rotors/II.rot rotors/III.rot rotors/I.pos
 ==68943== Memcheck, a memory error detector
 ==68943== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
 ==68943== Using Valgrind-3.14.0 and LibVEX; rerun with -h for copyright info
 ==68943== Command: ./enigma plugboards/I.pb reflectors/I.rf rotors/I.rot rotors/II.rot rotors/III.rot rotors/I.pos
 ==68943== 
 --68943-- run: /usr/bin/dsymutil "./enigma"
 ==68943== Use of uninitialised value of size 8
 ==68943==    at 0x100002598: Enigma::Enigma(int, char**) (enigma.cpp:17)
 ==68943==    by 0x100002F92: Enigma::Enigma(int, char**) (enigma.cpp:12)
 ==68943==    by 0x100000862: main (main.cpp:18)
 ==68943==  Uninitialised value was created by a stack allocation
 ==68943==    at 0x1000007D4: main (main.cpp:11)
 ==68943== 
 ==68943== Invalid write of size 8
 ==68943==    at 0x100002598: Enigma::Enigma(int, char**) (enigma.cpp:17)
 ==68943==    by 0x100002F92: Enigma::Enigma(int, char**) (enigma.cpp:12)
 ==68943==    by 0x100000862: main (main.cpp:18)
 ==68943==  Address 0x0 is not stack'd, malloc'd or (recently) free'd
 ==68943== 
 ==68943== 
 ==68943== Process terminating with default action of signal 11 (SIGSEGV)
 ==68943==  Access not within mapped region at address 0x0
 ==68943==    at 0x100002598: Enigma::Enigma(int, char**) (enigma.cpp:17)
 ==68943==    by 0x100002F92: Enigma::Enigma(int, char**) (enigma.cpp:12)
 ==68943==    by 0x100000862: main (main.cpp:18)
 ==68943==  If you believe this happened as a result of a stack
 ==68943==  overflow in your program's main thread (unlikely but
 ==68943==  possible), you can try to increase the size of the
 ==68943==  main thread stack using the --main-stacksize= flag.
 ==68943==  The main thread stack size used in this run was 10022912.
 ==68943== 
 ==68943== HEAP SUMMARY:
 ==68943==     in use at exit: 18,685 bytes in 166 blocks
 ==68943==   total heap usage: 187 allocs, 21 frees, 27,133 bytes allocated
 ==68943== 
 ==68943== LEAK SUMMARY:
 ==68943==    definitely lost: 0 bytes in 0 blocks
 ==68943==    indirectly lost: 0 bytes in 0 blocks
 ==68943==      possibly lost: 72 bytes in 3 blocks
 ==68943==    still reachable: 200 bytes in 6 blocks
 ==68943==         suppressed: 18,413 bytes in 157 blocks
 ==68943== Rerun with --leak-check=full to see details of leaked memory
 ==68943== 
 ==68943== For counts of detected and suppressed errors, rerun with: -v
 ==68943== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 1 from 1)
 Segmentation fault: 11

Thanks


Solution

  • Maybe you should take a look at: How do pointer to pointers work in C?

    Basically, I see that rotor is a pointer to pointer or possibly an array of pointers (since you initialize *rotor to NULL and later also set rotor[i] = new Rotor).

    Make sure you've initialized rotor properly. Is it pointing to a valid object? If not, you cannot expect *rotort = NULL /* or whatever value */; to work.