Reputation: 664
I am currently working on a project where I create a boost::asio::ip::udp::socket
and set it up to asynchronously listen for data from clients. I am using CMake to generate my project files for both Linux and Windows 10 deployment environments.
I have no problems with the code running on Linux with GCC and Clang, however, when I run in Release
mode with MSVC and Visual Studio than I get an access violation when the program is exiting.
Here is the error that I am getting:
Exception thrown at 0x00007FFB51954989 (ntdll.dll) in Weave_Server.exe: 0xC0000005: Access violation writing location 0x0000000000000024.
The stack shows that this exception is happening on the destruction of my NetworkManager
class:
EDIT: Here is the destrcutor for the object that has my udp::socket
and io_service
object. The full destructor gets called and the error is coming from somewhere in either the io_service
being destroyed or the udp::socket
being cleaned up. The udp::socket
is a std::shared_ptr
and will be destroyed when this function goes out of scope:
Through some research I know that ntdll.dll
is for debugging symbols of Visual Studio C++ projects, which makes sense why it would crash in Release mode (because debugging symbols shouldn't be loaded).
Why would the project be trying to access any debugging symbols from inside a release mode project? Is this a setting that I need to set in the CMake file?
Here is where I get boost in my CMakeLists.txt
for the project:
if ( MSVC )
set( Boost_USE_STATIC_LIBS ON )
else()
set( Boost_USE_STATIC_LIBS OFF )
endif()
set( Boost_DETAILED_FAILURE_MSG ON ) # Useful error messages from boost
set( Boost_USE_STATIC_RUNTIME OFF )
set( Boost_DEBUG OFF ) # Print debug info from find boost
FIND_PACKAGE( Boost COMPONENTS system regex REQUIRED )
if( Boost_FOUND )
message( STATUS "Success, Boost found!" )
else()
message( ERROR "Cannot find boost! Exiting..." )
return()
endif()
Another thing I can think of is changing if Boost is statically or dynamically linked, but I have changed the BOOST_USE_STATIC_LIBS
and the same error occurs in both settings.
Does anyone have any ideas as to why this exception is being thrown, or some settings to check for Debug vs Release that I am missing? Thanks
Upvotes: 1
Views: 792
Reputation: 664
This problem was happening because the io_service
object was being destroyed when my NetworkManager
destructor was going out of scope. This was problematic because I was using udp::socket::async_recvFrom
, which was still trying to access to the io_service
object.
The problem can be solved by either passing in a reference to the io_service
object and creating the socket via the default constructor, or, you can use a shared pointer to io_service
and pass in that reference if you don't want to create the sockets right away.
Upvotes: 1