1
votes

I'm using Visual Studio 2012 for my project and I'm a newbie at using boost, so I encountered with a runtime problem trying to initialize this:

FClient::FClient(const std::string & logName, const std::string & logPassword, udp::endpoint hostpoint) : mSocket(mService), mLogName(logName), mLogPassword(logPassword), mEndPoint(hostpoint)
{

}

Where mSocket is a boost udp socket, and mService is a boost asio io_service (both private members of FClient), and I guess the rest of the calls (which basically are string inits) are not useful to detect the error. I hope the error is on mSocket(mService) initialization which is of type:

boost::asio::ip::udp::socket(boost::asio::io_service)

Visual Studio call stack: http://pastebin.com/fjwWbhst

I'm using windows 8 with boost 1.5.6 and I'm defining: -D_WIN32_WINNT=0x0602

Thank you,

-lilEzek

2
impossible to say without a more detailed example. Can you create a minimal complete example to recreate the problem?Richard Hodges
Show please FClient definition (with its members)Igor R.
what is the error? Exception thrown? Crash?Sam Miller

2 Answers

5
votes

I know this is old, but I just had a similar issue and came across this post. I figured out the problem was that the socket was being initialized before the IO service was constructed. So when the socket constructor is called with the non-constructed IO service, it causes a segfault.

In C++, the order of construction of class members is determined by the order they are listed in the class declaration (usually in the .h file). So if, FClient is declared like this:

class FClient
{
    // ...
    boost::asio::ip::udp::socket mSocket;
    boost::asio::io_service mService;
    // ...
};

You get the a segfault during construction of FClient::mSocket. The fix is to swap the order of mSocket and mService:

class FClient
{
    // ...
    boost::asio::io_service mService;
    boost::asio::ip::udp::socket mSocket;
    // ...
};

This ensures mService is constructed before being passed to the mSocket constructor.

0
votes

I'm going to close this question as I fixed it doing mSocket a pointer, and initializing it somewhere else. Thank you anyway for the help given.