As we knows m_pRefCounters can be allocated from different execution units (my dll and Archiver dll in this case)
However MSVC compiles the following virtual function call into direct function call:
//RefCntAutoPtr.hpp
void Release() noexcept
{
if (m_pRefCounters)
m_pRefCounters->ReleaseWeakRef(); //will be optimized by MSVC
m_pRefCounters = nullptr;
m_pObject = nullptr;
}
This can lead to crash when my dll uses custom global operator_new override (mi_new to override pNewRefCounters allocation in this case) :
//RefCountedObjectImpl.hpp
pNewRefCounters = new RefCountersImpl{}; //this goes with `mi_new` from my dll and with ms crt `malloc` from archiver dll
Possible fix A:
Use custom allocator m_pAllocator to allocate RefCountersImpl when available.
Possible fix B:
Tell MSVC not to optimize the virtual function call
#pragma optimize( "", off )
void Release() noexcept
{
if (m_pRefCounters)
m_pRefCounters->ReleaseWeakRef(); //call archiver one when optimization off
m_pRefCounters = nullptr;
m_pObject = nullptr;
}
#pragma optimize( "", on )