Memory access violation error c++

1

I'm trying to exchange messages using multiple covert channels. So, basically, first i need to select the channel that i want to use for communication and then select the "destination_ip" of the user that i want to chat with and after that the

processMessage()

is called. Now, to move from one channel to another I have to close the existing connection and then open a new connection with the new channel that i want to use. My code below is modified to keep using the same channel after closing the connection and contain only the things that you need.

#include <channelmanager.hpp>
#include <thread>
#include <iostream>
#include <boost/test/unit_test.hpp>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include <stdio.h>
#include <string.h>
#include <fstream>
#include <openssl/hmac.h>

struct CommunicationFixture {
    CommunicationFixture() {
        channelmanager.setErrorStream(&cout);
        channelmanager.setOutputStream(&cout);
        destination_ip = "";
        channel_id = channelmanager.getChannelIDs()[0];

    }
    library::ChannelManager channelmanager;
    vector<string> last_adapters;
    string destination_ip;
    string channel_id = "";


    int processMessage(string message) {
        if (message.compare("exit") == 0) {
            channelmanager.closeConnection(destination_ip);
            return 1;
        }
        vector<string> arguments;
        boost::split(arguments, message, boost::is_any_of(" "), boost::token_compress_on);
        if (arguments[0].compare("argument") == 0) {
            if (arguments.size() < 2) {
                cout << "Not enough arguments" << endl;
                return 0;
            }
            string argument_list = arguments[1];
            for (unsigned int i = 2; i < arguments.size(); i++) {
                argument_list += " " + arguments[i];
            }
            channelmanager.setChannelArguments(destination_ip, argument_list);
            cout << "Set channel argument to '" << argument_list << "'." << endl;
            return 0;
        }
        if (message.compare("help") == 0) {
            cout << "Help not available in chat mode. Close chat first with 'exit'" << endl;
            return 0;
        }


        channelmanager.openConnection(destination_ip, channel_id);
        channelmanager.sendMessage(destination_ip, message);

        return 0;

    }

    int close(string destination){
        cout << "closing.." << endl;
        channelmanager.closeConnection(destination); //I believe i have the error because of this!
        return 0;
    }

};


BOOST_FIXTURE_TEST_SUITE(communication, CommunicationFixture)

BOOST_AUTO_TEST_CASE(basic_communication) {

    selectAdapterId(0);
    cout << "Test" << endl << endl;
    printCommands();
    cout << "Enter your command:" << endl;
    string command;
    int code = 0;
    while (code != 2) {
        std::getline(cin, command);
        code = processCommand(command);
        if (code == 1) {
            // chat
            cout << "chat started.." << endl;
            int chatCode = 0;


            while (chatCode != 1) {
                std::getline(cin, message);
                close(destination_ip);
                chatCode = processMessage(message);
                channelmanager.setErrorStream(&cout);
            }
            cout << "chat ended." << endl;
        }
    }



}

BOOST_AUTO_TEST_SUITE_END()

Note that, i think that the error happens due to the

function close()

because without it i don't get any errors. and the error doesn't happen immediately but after exchanging some messages. Here's the error:

unknown location(0): fatal error: in "communication/basic_communication": memory access violation at address: 0x00000024: no mapping at fault address communicationTest.cpp(325): last checkpoint: "basic_communication" test entry

c++
memory-access
asked on Stack Overflow Feb 27, 2018 by John123 • edited Feb 27, 2018 by John123

2 Answers

1

Memory access violation happen when you are trying to access to an unitialized variable, in this case the channelmanager.

I can only see that you initialize channelmanager in the processMessage() method and you are closing the connection before initializing the channelmanager as it happen in:

close(destination_ip);
chatCode = processMessage(message);

Either you change the initialization or do not close it before the processMessage() method.

answered on Stack Overflow Feb 27, 2018 by Pedro Pereira
0

Memory access violation is also called a segmentation fault (or segfault), occurs when the program tries to access a memory location that doesn't exist, or is otherwise inaccessible. We call this trying to access an illegal memory location. That memory is either non-existent or we aren't aren't allowed to touch it.

If the first input from user is 'exit', which is going to call

if (message.compare("exit") == 0) {
     channelmanager.closeConnection(destination_ip);
     return 1;
}  

In this case, destination_ip isn't initialised.

answered on Stack Overflow Feb 27, 2018 by Archie Yalakki • edited Feb 27, 2018 by Archie Yalakki

User contributions licensed under CC BY-SA 3.0