Skip to content

Commit 8103cc2

Browse files
xusheng6claude
andcommitted
[emulator] Start EmuRefCountObject at a reference count of 1
The reference-counted base started at 0 and relied on the create helper's AddAPIRef to reach 1, leaving the object destructible by any transient AddRef/Release during construction. Start at 1 (the birth reference handed to the creator) like core's RefCountObject, and have the create helper hand off that reference without an extra AddRef. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 3b559d8 commit 8103cc2

2 files changed

Lines changed: 9 additions & 5 deletions

File tree

plugins/emulator/core/llilemulator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3583,13 +3583,13 @@ bool LLILEmulator::StubFread(uint64_t)
35833583

35843584
BNLLILEmulator* BNCreateLLILEmulatorForView(BNBinaryView* view)
35853585
{
3586-
return EMU_API_OBJECT_REF(new LLILEmulator(new BinaryView(BNNewViewReference(view))));
3586+
return EMU_API_OBJECT_CREATE(new LLILEmulator(new BinaryView(BNNewViewReference(view))));
35873587
}
35883588

35893589

35903590
BNLLILEmulator* BNCreateLLILEmulator(BNLowLevelILFunction* il, BNBinaryView* view)
35913591
{
3592-
return EMU_API_OBJECT_REF(new LLILEmulator(new LowLevelILFunction(BNNewLowLevelILFunctionReference(il)), new BinaryView(BNNewViewReference(view))));
3592+
return EMU_API_OBJECT_CREATE(new LLILEmulator(new LowLevelILFunction(BNNewLowLevelILFunctionReference(il)), new BinaryView(BNNewViewReference(view))));
35933593
}
35943594

35953595

plugins/emulator/core/refcountobject.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,10 @@ namespace BinaryNinjaEmulator
2727
{
2828
public:
2929
std::atomic<int> m_refs;
30-
EmuRefCountObject() : m_refs(0) {}
30+
// Born with one reference (the one handed to whoever creates the object), mirroring
31+
// core's RefCountObject. Starting at 0 would leave the object destructible by any
32+
// transient AddRef/Release that happens before the creator takes its reference.
33+
EmuRefCountObject() : m_refs(1) {}
3134
virtual ~EmuRefCountObject() {}
3235

3336
void AddRef() { m_refs.fetch_add(1); }
@@ -44,12 +47,13 @@ namespace BinaryNinjaEmulator
4447

4548

4649
// Macro-like helpers to hand referenced objects across the external C ABI.
50+
// Hand a freshly-created object's birth reference to the caller (no extra AddRef; the
51+
// object is constructed with a reference count of 1).
4752
template <class T>
48-
static typename T::APIHandle EMU_API_OBJECT_REF(T* obj)
53+
static typename T::APIHandle EMU_API_OBJECT_CREATE(T* obj)
4954
{
5055
if (obj == nullptr)
5156
return nullptr;
52-
obj->AddAPIRef();
5357
return obj->GetAPIObject();
5458
}
5559

0 commit comments

Comments
 (0)