# Strange leak in C++11 code using Juce Core \[not a juce bug\]

**URL:** <https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817>\
**Category:** Windows\
**Created:** [November 1, 2015, 3:47pm UTC](https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817 "2015-11-01T15:47:20Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![mathieup](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@mathieup](https://forum.juce.com/u/mathieup)\
**Post date:** [November 1, 2015, 3:47pm UTC](https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817/1 "2015-11-01T15:47:20Z")

</div>

Hello,

Searching for a leak in some code, I found out the following:

The following code leaks when linked to juce\_core.cpp (with VS2012):

```
#include <thread>

void theThread()
{
}

int __stdcall WinMain (struct HINSTANCE__*, struct HINSTANCE__*, char*, int)
{
    std::thread * thread = new std::thread(theThread);

    thread->join();

    delete thread;

    return 0;
}
```

Why should this code be affected by juce\_core? (or how could this code affect juce\_core)?

Whatever, there seems to be something fishy with threads somewhere...

Any help would be appreciated! :)

Thanks! -Mathieu

```
The thread 0x5b4 has exited with code 0 (0x0).
Detected memory leaks!
Dumping objects ->
{202} normal block at 0x00F53EF8, 44 bytes long.
 Data: < > 01 00 00 00 00 00 00 00 00 00 00 00 0A 00 00 00
Object dump complete.
The thread 0x15a4 has exited with code 0 (0x0).
The program '[0x308] JuceDemo.exe' has exited with code 0 (0x0).
```

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![mathieup](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@mathieup](https://forum.juce.com/u/mathieup)\
**Post date:** [November 1, 2015, 3:57pm UTC](https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817/2 "2015-11-01T15:57:45Z")

</div>

By the way, this is not a timing issue.

The same behavior happens with the following code:

```
#include <atomic>
#include <condition_variable>
#include <mutex>
#include <thread>

std::atomic<bool> finished;
std::mutex mutex;
std::condition_variable conditionVariable;

void theThread()
{
    // wait one second
    std::unique_lock<std::mutex> lock(mutex);
    conditionVariable.wait_for(lock, std::chrono::seconds(1));

    finished = true;
}

int __stdcall WinMain (struct HINSTANCE__*, struct HINSTANCE__*, char*, int)
{
    std::thread * thread = new std::thread(theThread);

    while (finished.load() == false)
        std::this_thread::yield();

    thread->join();

    delete thread;

    return 0;
}
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 1, 2015, 4:47pm UTC](https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817/3 "2015-11-01T16:47:00Z")

</div>

The MS leak detector only runs because code in juce\_core invokes it on shutdown. Not seeing the message when you don't use&nbsp;juce\_core doesn't mean there isn't a leak, it just means its not being checked at all.

It's most likely just a win32 message left on the queue at exit, or a static variable somewhere that's not being deleted before the leak detector runs (which can happen before all other statics are destroyed).

(And come on, please..&nbsp;even in test code there's no excuse to ever write 'delete', especially when you're explicitly using C++11 classes! Let's make this forum a place where we ALL try to set a good example&nbsp;to anyone reading it!)

---

<div class="post-metadata">

**Author:** ![mathieup](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@mathieup](https://forum.juce.com/u/mathieup)\
**Post date:** [November 1, 2015, 6:28pm UTC](https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817/4 "2015-11-01T18:28:07Z")

</div>

Ok ok, no delete! :)

Same problem here:

```
void theThread()
{
    // wait one second
    std::unique_lock<std::mutex> lock(mutex);
    conditionVariable.wait_for(lock, std::chrono::seconds(1));

    finished = true;
}

int __stdcall WinMain (struct HINSTANCE__*, struct HINSTANCE__*, char*, int)
{
    std::thread thread(theThread);

    while (finished.load() == false)
        std::this_thread::yield();

    thread.join();

    _CrtCheckMemory();

    return 0;
}
```

And I added a call to \_CrtCheckMemory() to check for leaks.

There is something strange.

```
Log without linking juce_core.cpp
'JuceDemo.exe' (Win32): Loaded 'C:\Windows\SysWOW64\advapi32.dll'. Cannot find or open the PDB file.
The thread 0x12b4 has exited with code 0 (0x0).
The thread 0x1594 has exited with code 0 (0x0).
The program '[0x1138] JuceDemo.exe' has exited with code 0 (0x0).

vs

Log linking juce_core.cpp
'JuceDemo.exe' (Win32): Loaded 'C:\Windows\SysWOW64\msctf.dll'. Cannot find or open the PDB file.
The thread 0x4b4 has exited with code 0 (0x0).
Detected memory leaks!
Dumping objects ->
{204} normal block at 0x00BDAF00, 44 bytes long.
 Data: < > 01 00 00 00 00 00 00 00 00 00 00 00 0A 00 00 00
Object dump complete.
The thread 0x119c has exited with code 0 (0x0).
The program '[0x64] JuceDemo.exe' has exited with code 0 (0x0).
```

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 1, 2015, 7:48pm UTC](https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817/5 "2015-11-01T19:48:34Z")

</div>

Yes. Like I said, it'll be a static object somewhere.

Obviously if you run the leak checker in your main function - i.e.&nbsp;before the program's static destructors have been called -&nbsp;then that's exactly what you'd expect to see.

---

<div class="post-metadata">

**Author:** ![mathieup](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@mathieup](https://forum.juce.com/u/mathieup)\
**Post date:** [November 1, 2015, 11:51pm UTC](https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817/6 "2015-11-01T23:51:15Z")

</div>

Adding a breakpoint in \_nh\_malloc\_dbg\_impl I found where the leak comes from:

```
int _Mtx_init(_Mtx_t *mtx, int type)
    { /* initialize mutex */
    _Mtx_t mutex;
    *mtx = 0;

    if ((mutex = (mtx_t)calloc(1, sizeof (struct _Mtx_internal_imp_t))) == 0)
```

with stack

```
JuceDemo.exe!calloc(unsigned int nNum, unsigned int nSize) Line 56 C++
     JuceDemo.exe!_Mtx_init(_Mtx_internal_imp_t * * mtx, int type) Line 24 C++
     JuceDemo.exe!init_at_thread_exit_mutex() Line 28 C
     JuceDemo.exe!_Call_once(char * cntrl, void (void) * func) Line 41 C
     JuceDemo.exe!_Cnd_do_broadcast_at_thread_exit() Line 107 C
     JuceDemo.exe!_Call_func(void * _Data) Line 70 C++
     JuceDemo.exe!_callthreadstartex() Line 354 C
     JuceDemo.exe!_threadstartex(void * ptd) Line 337 C
```

but I haven't found yet how this block gets freed when not linking with Juce. I'll keep investigating.

Another strange thing I found out is that the same code with Juce v3.1.1 generates a 72 byte leak, while with the current Juce v3.2.0 the leak size is 44 bytes...

&nbsp;

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [November 2, 2015, 8:04am UTC](https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817/7 "2015-11-02T08:04:29Z")

</div>

I tried to explain twice, did you not read my answers?? THIS IS NOT A REAL MEMORY LEAK!

Don't "keep investigating", it's a waste of your time! Go and get on with some real programming instead!

---

<div class="post-metadata">

**Author:** ![mathieup](https://avatars.discourse-cdn.com/v4/letter/m/82dd89/32.png) [@mathieup](https://forum.juce.com/u/mathieup)\
**Post date:** [November 3, 2015, 9:22pm UTC](https://forum.juce.com/t/strange-leak-in-c-11-code-using-juce-core-not-a-juce-bug/15817/8 "2015-11-03T21:22:18Z")

</div>

No point commenting the remarks.

To remove the annoying memory leak log when using std::thread together with Juce, try adding the following line at the beginning of main:

```
_CrtSetDbgFlag(_CRTDBG_LEAK_CHECK_DF);
```

The documentation states that using this flag performs automatic leak checking at program exit. In this case it also prevents the false leak detection.

&nbsp;
