简体   繁体   English

内存访问冲突错误C ++

[英]Memory access violation error c++

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 因此,基本上,首先,我需要选择要用于通信的频道,然后选择要与之聊天的用户的“ destination_ip”,然后

processMessage() 而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() 函数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 未知位置(0):致命错误:在“ communication / basic_communication”中:地址处的内存访问冲突:0x00000024:在故障地址communicationTest.cpp(325)处没有映射:最后一个检查点:“ basic_communication”测试条目

Memory access violation happen when you are trying to access to an unitialized variable, in this case the channelmanager . 当您尝试访问单元化变量(在这种情况下为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: 我只能看到你初始化channelmanagerprocessMessage()方法和你正在关闭初始化之前的连接channelmanager ,因为它在发生:

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

Either you change the initialization or do not close it before the processMessage() method. 您可以更改初始化,或者不要在processMessage()方法之前关闭初始化。

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. 内存访问冲突也称为分段错误(或segfault),发生在程序尝试访问不存在或无法访问的内存位置时。 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 如果用户的第一个输入是“ exit”,这将被调用

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

In this case, destination_ip isn't initialised. 在这种情况下,destination_ip不会被初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM