Can heap corruption be caused by linking issues?

2

I am getting an access violation in my program when running some code that belongs to a library. My program statically links to the library. I'm able to run that code in other projects without issues, but for some reason it crashes in my program. I put the code at the very beginning of the program to ensure no other code has been executed and it still crashes. I don't think it's an issue with the library since that runs fine in other contexts. I have no idea why it's crashing and I've been trying to figure it out for two weeks now. Here is the more detailed post I've copied to the library's github page:

I'm trying to use the http_listener in an MFC project built in visual studio 2017 with platform toolset v141. It runs fine until executing the code that links to the cpprestsdk. The cpprestsdk was installed using vcpkg, which built the cpprestsdk using the vc141 platform toolset.

My project has no known preexisting heap corruption issues, so the fact that this is causing a heap corruption is confusing.

Here is the offending line of code:

BOOL CAuthenticationServerApp::InitInstance()
{
    CWinApp::InitInstance();

    utility::string_t port = U("34568");
    utility::string_t address = U("http://127.0.0.1:");
    address.append(port);

    web::uri_builder uri(address);

    auto addr = uri.to_uri().to_string();
---->std::unique_ptr<http_listener> listener = std::make_unique<http_listener>(addr);
    listener->open().wait();
    ...

The first thing my program executes is this code. There is no other code before it, so nothing that would potentially cause a corrupted heap, so I'm thinking it has something to do with the library itself.

Error message:

Exception thrown at 0x005B90F8 in AuthenticationServer.exe: 0xC0000005: Access violation writing location 0x0B39D000. Unhandled exception thrown: write access violation.

Here is the call stack where the exception is being thrown. It's crashing when trying to declare a variable in the http_listener_impl contsructor:

    AuthenticationServer.exe!std::_Ptr_base<Concurrency::details::_Task_impl<unsigned char> >::_Ptr_base<Concurrency::details::_Task_impl<unsigned char> >() Line 1158  C++
    AuthenticationServer.exe!std::shared_ptr<Concurrency::details::_Task_impl<unsigned char> >::shared_ptr<Concurrency::details::_Task_impl<unsigned char> >(void * __formal) Line 1277 C++
    AuthenticationServer.exe!Concurrency::task<unsigned char>::task<unsigned char>() Line 2963  C++
    AuthenticationServer.exe!Concurrency::task<void>::task<void>() Line 3985    C++
    AuthenticationServer.exe!web::http::experimental::listener::details::http_listener_impl::http_listener_impl(web::uri address) Line 58   C++
    AuthenticationServer.exe!utility::details::make_unique<web::http::experimental::listener::details::http_listener_impl,web::uri>(web::uri && arg1) Line 389  C++
    AuthenticationServer.exe!web::http::experimental::listener::http_listener::http_listener(web::uri address) Line 222 C++
    [External Code] 
    AuthenticationServer.exe!CAuthenticationServerApp::InitInstance() Line 79   C++
    AuthenticationServer.exe!AfxWinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 37   C++
    AuthenticationServer.exe!WinMain(HINSTANCE__ * hInstance, HINSTANCE__ * hPrevInstance, char * lpCmdLine, int nCmdShow) Line 26  C++
    [External Code] 

This code actually runs fine if I place it in a new MFC project or another similar sized MFC project. Is it possible that there's an issue with the library, or is it something to do with my code? The program does have some global variables, which I checked, and none of them contain code that allocates or frees memory on the heap in their constructors. So I don't know how the heap is getting corrupted. The project links to a few other libraries, and I've ensured that they were all built using the same platform toolset. The libraries have no known issues and have been used for years. I've placed the http_listener code in another project that is almost identical to this one, with the same dependencies and it runs fine.

c++
access-violation
heap-corruption
asked on Stack Overflow Mar 30, 2019 by Alex • edited Mar 30, 2019 by Alex

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0