Home > OS >  Invalid Read Of size 8 when passing a string to a function
Invalid Read Of size 8 when passing a string to a function

Time:12-18

Im a second year CS student and Im attempting to make a hangman game for fun during my winter break. Ive implemented the beginning of a menu class here, and yes I know this is too much oop for a project of this scope but I intent to reuse all these classes.

Anyways valgrind is telling me an Invalid read of size 8 and it returns the following lines in order:

menu::getName (menu.cpp:13) (this is at menu.cpp\getName()\return name;)

menu::print() (menu.cpp:21) (this is at menu.cpp\print()\cout<<". return to "<getName();)

main (main.cppp:28) (this is at main.cpp\main()\thisMenu->print();)

Im usually very good at solving these types of problems, and I searched the web but I couldnt find a solution and Im completely stumped. I feel like it could be a simple issue and Im missing something but Im not sure. Any help is greatly appreciated! Thanks!

menu.cpp:

#include <vector>
using namespace std;
//contains a list of menus, allowing for us to navigate menus and keep track of where we are via linked list
class menu{
    private:
    //we will keep track of which menus this one can access via a vector and a parent pointer
    vector<menu*> options;
    string name="";
    menu* parent;
    public:
    string getName(){
        return name;
    }
    void insert(menu* obj){
        options.push_back(obj);
    }
    //print all the menus this one connects to, i.e. the menus the user can access from here
    void print(){
        int size=options.size();
        **if (parent!=nullptr){
            cout<<". return to "<<parent->getName();
        }** 
        for(int i=0; i<size; i  ){
            cout<<i 2<<". "<<options[i]->getName()<<endl;
        }
    }
    menu* nextMenu(int input){
        try{
            return(options.at(input));
        }
        catch(...){
            return this;
        }
    }
    menu(string str, menu *parentIn){
        parent=parentIn;
        name=str;
    }
};

main.cpp:

#include "SkipList.h"
#include "menu.cpp"
#include <string>
using namespace std;
int main(int argc, char** argv) {
    SkipList *letterBank=new SkipList;
    vector<string> wordbank();
    //fill the letter bank with all lower case letters
    for(char currLetter=97; currLetter<123; currLetter  ){
        Key currKey=to_string((int)currLetter);
        Value currVal=to_string(currLetter);
        letterBank->insert(currKey, currVal);
    }
    menu *MainMenu=new menu(string("Main Menu"), nullptr);
    menu *NumOfPlayers=new menu(string("Number of Players"), MainMenu);
    menu *PlayerSelect=new menu(string("Player Select"), NumOfPlayers);
    menu *Game=new menu(string("Game"), NumOfPlayers);
    menu *WordBankMenu=new menu(string("Word Bank Modification"), MainMenu);
    MainMenu->insert(NumOfPlayers);
    NumOfPlayers->insert(PlayerSelect);
    PlayerSelect->insert(Game);
    Game->insert(nullptr);
    MainMenu->insert(WordBankMenu);
    menu *thisMenu= MainMenu;
    int input;
    while(thisMenu!=nullptr){
        thisMenu->print();
        cin>>input;
        thisMenu=thisMenu->nextMenu(input);
    }
}

CodePudding user response:

If you look at this line:

cout<<". return to "<<parent->getName();

You access the parent's name here;

But for MainMenu, parent is nullptr, therefore it's an invalid access!

The line needs to be made conditional on parent being different from nullptr.

  • Related