cpprestsdk icon indicating copy to clipboard operation
cpprestsdk copied to clipboard

Annoying CRT memory leak reports with 2.7.0 build from NuGet

Open vadz opened this issue 10 years ago • 3 comments

After upgrading a simple project using the library from VS2013 with C++ REST SDK 2.1.0 to VS2015 with 2.7.0 I started getting

Detected memory leaks!
Dumping objects ->
{345} normal block at 0x014A6970, 12 bytes long.
 Data: <       Yp  Y> 01 00 00 00 D0 9D E8 59 70 B4 F4 59 
{344} normal block at 0x014A5C28, 16 bytes long.
 Data: <piJ             > 70 69 4A 01 00 00 00 00 00 00 00 00 00 00 00 00 
{343} normal block at 0x014A5BA8, 20 bytes long.
 Data: <    (\J         > 01 00 00 00 28 5C 4A 01 01 00 00 00 04 00 00 00 
Object dump complete.

on application exit. The allocation stack for the allocation 343 looks like this:

>   ucrtbased.dll!malloc(unsigned int size=0x00000014) Line 18  C++
    cpprest140d_2_7.dll!default_malloc_ex(unsigned int num=0x00000014, const char * file=0x5f226ba4, int line=0x000000a2) Line 79   C
    cpprest140d_2_7.dll!CRYPTO_malloc(int num=0x00000014, const char * file=0x5f226ba4, int line=0x000000a2) Line 342   C
    cpprest140d_2_7.dll!sk_new(int(*)(const void *, const void *) c=0x5f132460) Line 162    C
    cpprest140d_2_7.dll!load_builtin_compressions() Line 483    C
    cpprest140d_2_7.dll!SSL_COMP_get_compression_methods() Line 1949    C
    cpprest140d_2_7.dll!SSL_library_init() Line 153 C
    cpprest140d_2_7.dll!boost::asio::ssl::detail::openssl_init_base::do_init::do_init() Line 40 C++
    cpprest140d_2_7.dll!boost::asio::ssl::detail::openssl_init_base::instance() Line 131    C++
    cpprest140d_2_7.dll!boost::asio::ssl::detail::openssl_init<1>::openssl_init<1>() Line 60    C++
    cpprest140d_2_7.dll!`dynamic initializer for 'boost::asio::ssl::detail::openssl_init<1>::instance_''() Line 90  C++
    ucrtbased.dll!_initterm(void(*)() * first=0x5f206000, void(*)() * last=0x5f206684) Line 22  C++

so it seems that the real problem is that ASIO initializes OpenSSL by calling SSL_library_init(), but SSL_COMP_free_compression_methods() is not being called anywhere, however it's impossible to work around this by calling it from the application code as OpenSSL seems to be statically linked in, so perhaps it could be done from Casablanca itself?

vadz avatar Feb 03 '16 02:02 vadz

Yes, this looks like it would need to be done inside Casablanca itself.

ras0219-msft avatar Feb 04 '16 23:02 ras0219-msft

If you do not need websockets on Windows, you can work around the memory leak and produce a leaner SDK 2.7.0 by removing the dependencies on boost, zlib and openssl.

Just define CPPREST_EXCLUDE_WEBSOCKETS and remove the import directives for the unneeded libraries within the respective casablanca project file.

megaposer avatar Feb 08 '16 08:02 megaposer

Thanks, unfortunately I do use WebSockets and TLS, so I can't get rid of OpenSSL.

vadz avatar Feb 08 '16 13:02 vadz