Skip to content

RefCountersImpl is destroyed in a different execution unit than the one in which it was created #704

@hzqst

Description

@hzqst

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) :

Image Image
//RefCountedObjectImpl.hpp

pNewRefCounters = new RefCountersImpl{}; //this goes with `mi_new` from my dll and with ms crt `malloc` from archiver dll

Possible fix A:

Image

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 )

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions