mirror of
https://github.com/oven-sh/bun
synced 2026-02-09 18:38:55 +00:00
Calling delete on yoga::Node during GC finalization is unsafe because the GC sweeps objects in arbitrary order and the allocator behavior on musl can cause null-pointer dereferences. The LSAN suppressions in test/leaksan.supp handle the leak detection false positives. This reverts the destructor and replaceYogaNode changes from660e4b3cwhile keeping the LSAN suppressions fromc17c345d.
84 lines
1.9 KiB
C++
84 lines
1.9 KiB
C++
#include "YogaNodeImpl.h"
|
|
#include "JSYogaNode.h"
|
|
#include "JSYogaConfig.h"
|
|
#include "JSYogaNodeOwner.h"
|
|
#include <yoga/Yoga.h>
|
|
#include <wtf/HashSet.h>
|
|
#include <wtf/Lock.h>
|
|
|
|
namespace Bun {
|
|
|
|
Ref<YogaNodeImpl> YogaNodeImpl::create(YGConfigRef config)
|
|
{
|
|
return adoptRef(*new YogaNodeImpl(config));
|
|
}
|
|
|
|
YogaNodeImpl::YogaNodeImpl(YGConfigRef config)
|
|
{
|
|
if (config) {
|
|
m_yogaNode = YGNodeNewWithConfig(config);
|
|
} else {
|
|
m_yogaNode = YGNodeNew();
|
|
}
|
|
|
|
// Store this C++ wrapper in the Yoga node's context
|
|
YGNodeSetContext(m_yogaNode, this);
|
|
}
|
|
|
|
YogaNodeImpl::~YogaNodeImpl()
|
|
{
|
|
m_yogaNode = nullptr;
|
|
}
|
|
|
|
void YogaNodeImpl::setJSWrapper(JSYogaNode* wrapper)
|
|
{
|
|
// Only increment ref count if we don't already have a wrapper
|
|
// This prevents ref count leaks if setJSWrapper is called multiple times
|
|
if (!m_wrapper) {
|
|
// Increment ref count for the weak handle context
|
|
this->ref();
|
|
}
|
|
|
|
// Create weak reference with our JS owner
|
|
m_wrapper = JSC::Weak<JSYogaNode>(wrapper, &jsYogaNodeOwner(), this);
|
|
}
|
|
|
|
void YogaNodeImpl::clearJSWrapper()
|
|
{
|
|
m_wrapper.clear();
|
|
}
|
|
|
|
void YogaNodeImpl::clearJSWrapperWithoutDeref()
|
|
{
|
|
// Clear weak reference without deref - used by JS destructor
|
|
// when WeakHandleOwner::finalize will handle the deref
|
|
m_wrapper.clear();
|
|
}
|
|
|
|
JSYogaNode* YogaNodeImpl::jsWrapper() const
|
|
{
|
|
return m_wrapper.get();
|
|
}
|
|
|
|
JSYogaConfig* YogaNodeImpl::jsConfig() const
|
|
{
|
|
// Access config through JS wrapper's WriteBarrier - this is GC-safe
|
|
if (auto* jsWrapper = m_wrapper.get()) {
|
|
return jsCast<JSYogaConfig*>(jsWrapper->m_config.get());
|
|
}
|
|
return nullptr;
|
|
}
|
|
|
|
YogaNodeImpl* YogaNodeImpl::fromYGNode(YGNodeRef nodeRef)
|
|
{
|
|
if (!nodeRef) return nullptr;
|
|
return static_cast<YogaNodeImpl*>(YGNodeGetContext(nodeRef));
|
|
}
|
|
|
|
void YogaNodeImpl::replaceYogaNode(YGNodeRef newNode)
|
|
{
|
|
m_yogaNode = newNode;
|
|
}
|
|
|
|
} // namespace Bun
|