octane92
octane92

Reputation: 49

Access violation exception when using socketio-client-c++ and boost asio io_service

I'm trying to use io_service from boost asio library (1.61) with socketio-client-c++, but my code throws an access violation exception, and after hours of debugging I could'nt figure out what is the cause. The exception is thrown at the following location in win_mutex.hpp:

void lock()
{
 -> ::EnterCriticalSection(&crit_section_);
}

The code is very simple:

int main(int argc, char* argv[]) {
    boost::asio::io_service io_service;
    auto io_service_work = std::make_shared<boost::asio::io_service::work>      (io_service);

    sio::client client_;
    client_.connect("http://localhost:1337");

    // io_service.run();
}

I want to use io_service from main, and socketio also uses an io_service internally, which might be the cause of the problem.

You can download the full project with socketio source and binary included here: http://stackoverflow-sehe.s3.amazonaws.com/7f1afa6a-883f-4941-8371-31ccbd8514a5/socket-io.zip ¹

Any suggestion?

¹ link redacted; links (was 88M, now 443k)

Upvotes: 1

Views: 450

Answers (2)

Blz
Blz

Reputation: 31

I have converted the socketio project to Dll, turned off boost auto link, and manually added libboost_system-vc140-mt-gd-1_61.lib as additional dependency. Then the code works as intended.

However, if I switch back the socketio project to static library, then the same symptom occurs as described in the question.

Please check the Dll based project files here: http://ginf.hu/socketio/socketiotest.zip

Upvotes: 1

sehe
sehe

Reputation: 393134

Because the line simply calls a public Win32 API with the address of something, all that could cause this to occur is when this is pointing at random something (e.g. a destructed win_mutex, something else, protected memory or even nullptr).

This indicates Undefined Behaviour. UB has a gazillion possible sources, and your code is not enough to draw any conclusions. But in Asio, 99% of the time it is caused by not keeping the objects around long enough for the asynchronous operations on it to complete.

Upvotes: 1

Related Questions