Skip to content

memory leak on Windows #352

Open
Open
@AndrewMagpie

Description

@AndrewMagpie

In a Visual Studio Windows C++ command line project I get memory leaks for a simple client:

    sio::client s;
    s.connect("http://blah.haha");

These are defined:

#define ASIO_STANDALONE
#define _WEBSOCKETPP_CPP11_STL_
#define _WEBSOCKETPP_CPP11_FUNCTIONAL_

Using the latest client code with OpenSSL 1.1.1.m:

OPENSSL_VERSION_NUMBER=0x101010dfL
OPENSSL_API_COMPAT=0x101010dfL

I enable mem leak reporting by calling this:

void DetectMemoryLeaks()
{
    _HFILE hReportFile = _CRTDBG_FILE_STDERR;

    //Always check for memleaks in debug builds.
    // Calls _CrtDumpMemoryLeaks() on program exit.
    _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF);

    //const int iReportMode = _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_WNDW | _CRTDBG_MODE_FILE;
    int iReportMode = _CRTDBG_MODE_DEBUG | _CRTDBG_MODE_FILE;

    if (GetConsoleWindow() == nullptr)
        iReportMode |= _CRTDBG_MODE_WNDW;

    //Warning: (memleaks)
    _CrtSetReportMode(_CRT_WARN, iReportMode);
    _CrtSetReportFile(_CRT_WARN, hReportFile);

    //Errors:
    _CrtSetReportMode(_CRT_ERROR, iReportMode);
    _CrtSetReportFile(_CRT_ERROR, hReportFile);

    //Asserts:
    _CrtSetReportMode(_CRT_ASSERT, iReportMode);
    _CrtSetReportFile(_CRT_ASSERT, hReportFile);
}

The leaks I get are:

Detected memory leaks!
Dumping objects ->
{4353} normal block at 0x0000023D9CAB9F10, 48 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{4352} normal block at 0x0000023D9CB22D60, 64 bytes long.
 Data: <    =           > 10 9F AB 9C 3D 02 00 00 00 00 00 00 00 00 00 00 
{4349} normal block at 0x0000023D9CB24E60, 264 bytes long.
 Data: <                > 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
{4348} normal block at 0x0000023D9CB24D80, 160 bytes long.
 Data: < `              > 80 60 A2 8F F7 7F 00 00 00 00 00 00 00 00 00 00 
{4347} normal block at 0x0000023D9CB24C30, 264 bytes long.
 Data: <i] +  BT'}   *d > 69 5D B6 2B F4 0B 42 54 27 7D 95 AD 96 2A 64 BA 
{4346} normal block at 0x0000023D9CB24AE0, 264 bytes long.
 Data: <i] +  BT'}   *d > 69 5D B6 2B F4 0B 42 54 27 7D 95 AD 96 2A 64 BA 
{4345} normal block at 0x0000023D9CB24A00, 160 bytes long.
 Data: <@d              > 40 64 A2 8F F7 7F 00 00 00 00 00 00 00 00 00 00 
{4344} normal block at 0x0000023D9CB24920, 160 bytes long.
 Data: < `              > 80 60 A2 8F F7 7F 00 00 00 00 00 00 00 00 00 00 
{4343} normal block at 0x0000023D9CB24760, 376 bytes long.
 Data: <            =   > 00 00 00 00 00 00 00 00 00 0E B2 9C 3D 02 00 00 
{4342} normal block at 0x0000023D9CAB9420, 48 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
{4341} normal block at 0x0000023D9CB225E0, 64 bytes long.
 Data: <    =           > 20 94 AB 9C 3D 02 00 00 00 00 00 00 00 00 00 00 
{4338} normal block at 0x0000023D9CB24610, 264 bytes long.
 Data: <                > 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F 
{4337} normal block at 0x0000023D9CB24530, 160 bytes long.
 Data: < `              > 80 60 A2 8F F7 7F 00 00 00 00 00 00 00 00 00 00 
{4336} normal block at 0x0000023D9CB243E0, 264 bytes long.
 Data: <  ,     X =4 J  > A6 9C 2C BF F5 E0 F6 91 58 84 3D 34 EF 4A A2 80 
{4335} normal block at 0x0000023D9CB24290, 264 bytes long.
 Data: <  ,     X =4 J  > A6 9C 2C BF F5 E0 F6 91 58 84 3D 34 EF 4A A2 80 
{4334} normal block at 0x0000023D9CB241B0, 160 bytes long.
 Data: <@d              > 40 64 A2 8F F7 7F 00 00 00 00 00 00 00 00 00 00 
{4333} normal block at 0x0000023D9CB21DA0, 160 bytes long.
 Data: < `              > 80 60 A2 8F F7 7F 00 00 00 00 00 00 00 00 00 00 
{4332} normal block at 0x0000023D9CB21650, 376 bytes long.
 Data: <            =   > 00 00 00 00 00 00 00 00 00 0E B2 9C 3D 02 00 00 
{3536} normal block at 0x0000023D9CB0F720, 12 bytes long.
 Data: <            > 00 00 00 00 01 00 00 00 01 00 00 00 
{3535} normal block at 0x0000023D9CB0FEA0, 520 bytes long.
 Data: <                > 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 
Object dump complete.

Leaks can trigger break points when calling: _CrtSetBreakAlloc(3535);

The {3535} and {3536} leaks is triggered from context::context from this source:

context::context(context::method m)
  : handle_(0)
{
    ::ERR_clear_error();

The {4332} leak is triggered from SSL_CTX_new in context::context from this source:

// Any supported TLS version.
#if (OPENSSL_VERSION_NUMBER >= 0x10100000L) && !defined(LIBRESSL_VERSION_NUMBER)
  case context::tls:
    handle_ = ::SSL_CTX_new(::TLS_method());

I'm linking against the static versions of OpenSSL:

libssl_static.lib
libcrypto_static.lib

The memory leaks do not occur when linked against the non-static versions of OpenSSL:

libssl.lib
libcrypto.lib

When the above libs are used the following DLLs must exist in the same location as the executable:

libcrypto-1_1-x64.dll
libssl-1_1-x64.dll

But this is what I'm trying to avoid, (having 3rd party DLLs).

So why does it leak for the static libraries of OpenSSL but not for the non-static libraries?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions