<div dir="ltr">Let's not do this. It's really best for global mutexes to live for the entire process lifetime. Initializing them and deinitializing them just asks for races. Perhaps you can avoid this problem by pinning your LLVM DLL in memory by LoadLibrary'ing it and dropping the reference.</div><br><div class="gmail_quote"><div dir="ltr">On Mon, Oct 15, 2018 at 3:54 AM Sotkin, Alexey via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">







<div lang="RU" link="#0563C1" vlink="#954F72">
<div class="m_5837773912688951461WordSection1">
<p class="MsoNormal"><span lang="EN-US">Hi,<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">In <span class="m_5837773912688951461SpellE">
llvm_shutdown</span> function we destroy all objects in the <span class="m_5837773912688951461SpellE">
StaticList</span>. This procedure as well as <span class="m_5837773912688951461SpellE">RegisterManagedStatic</span> are guarded by
<span class="m_5837773912688951461SpellE">ManagedStaticMutex</span> which is a static pointer visible only in ManagedStatic.cpp. Memory for the
<span class="m_5837773912688951461SpellE">mutex</span> is allocated at <span class="m_5837773912688951461SpellE">initializeMutex</span> function and we never free this memory. From my experience this can cause a memory leak problem if we create a
<span class="m_5837773912688951461SpellE">dll</span> linked with LLVM libraries and the <span class="m_5837773912688951461SpellE">
dll</span> gets loaded(<span class="m_5837773912688951461SpellE">LoadLibrary</span> Windows API) and unload(<span class="m_5837773912688951461SpellE">FreeLibrary</span> Windows API) at runtime many times.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">My proposal is to provide
<span class="m_5837773912688951461SpellE">deleteManagedStaticMutex</span> LLVM API, so we can deallocate the
<span class="m_5837773912688951461SpellE">mutex</span> from <span class="m_5837773912688951461SpellE">DllMain</span> on Windows. Please take a look at
<a href="https://reviews.llvm.org/D52425" target="_blank">https://reviews.llvm.org/D52425</a> <u></u>
<u></u></span></p>
<p class="MsoNormal"><span lang="EN-US">I also think it’s worth adding
<span class="m_5837773912688951461SpellE">deleteManagedStaticMutex</span> to <span class="m_5837773912688951461SpellE">llvm_shutdown_obj’s</span> destructor. I have attached
<span class="m_5837773912688951461SpellE">valgrind</span> logs with and without deleting <span class="m_5837773912688951461SpellE">
ManagedStaticMutex</span>.<u></u><u></u></span></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
<p class="MsoNormal"><i><span lang="EN-US">Best regards.<u></u><u></u></span></i></p>
<p class="MsoNormal"><i><span lang="EN-US">Alexey Sotkin.<u></u><u></u></span></i></p>
<p class="MsoNormal"><span lang="EN-US"><u></u> <u></u></span></p>
</div>
<p><br>--------------------------------------------------------------------<br>Joint Stock Company Intel A/O<br>Registered legal address: Krylatsky Hills Business Park, <br>17 Krylatskaya Str., Bldg 4, Moscow 121614, <br>Russian Federation</p><p>This e-mail and any attachments may contain confidential material for<br>the sole use of the intended recipient(s). Any review or distribution<br>by others is strictly prohibited. If you are not the intended<br>recipient, please contact the sender and delete all copies.</p>
</div>

_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div>