Skip to content

Commit

Permalink
fix: Fix runAsync not properly retaining/releasing Frame (#2829)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrousavy committed May 1, 2024
1 parent 96c2ecf commit e75bb9f
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 22 deletions.
3 changes: 3 additions & 0 deletions package/ios/FrameProcessors/Frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ NS_ASSUME_NONNULL_BEGIN
- (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOrientation)orientation;
- (instancetype)init NS_UNAVAILABLE;

- (void)incrementRefCount;
- (void)decrementRefCount;

@property(nonatomic, readonly) CMSampleBufferRef buffer;
@property(nonatomic, readonly) UIImageOrientation orientation;

Expand Down
7 changes: 5 additions & 2 deletions package/ios/FrameProcessors/Frame.m
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,15 @@ - (instancetype)initWithBuffer:(CMSampleBufferRef)buffer orientation:(UIImageOri
if (self) {
_buffer = buffer;
_orientation = orientation;
CFRetain(buffer);
}
return self;
}

- (void)dealloc {
- (void)incrementRefCount {
CFRetain(_buffer);
}

- (void)decrementRefCount {
CFRelease(_buffer);
}

Expand Down
5 changes: 0 additions & 5 deletions package/ios/FrameProcessors/FrameHostObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

#import <CoreMedia/CMSampleBuffer.h>
#import <jsi/jsi.h>
#import <mutex>

#import "Frame.h"

Expand All @@ -29,8 +28,4 @@ class JSI_EXPORT FrameHostObject : public jsi::HostObject {

private:
Frame* getFrame();

private:
std::mutex _mutex;
size_t _refCount = 0;
};
18 changes: 3 additions & 15 deletions package/ios/FrameProcessors/FrameHostObject.mm
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@
}

Frame* FrameHostObject::getFrame() {
Frame* frame = this->frame;
if (frame == nil || !frame.isValid) {
if (!frame.isValid) [[unlikely]] {
throw std::runtime_error("Frame is already closed! "
"Are you trying to access the Image data outside of a Frame Processor's lifetime?\n"
"- If you want to use `console.log(frame)`, use `console.log(frame.toString())` instead.\n"
Expand Down Expand Up @@ -99,25 +98,14 @@
// Internal methods
if (name == "incrementRefCount") {
auto incrementRefCount = JSI_FUNC {
std::lock_guard lock(this->_mutex);

// Increment our self-counted ref count by one.
_refCount++;
[frame incrementRefCount];
return jsi::Value::undefined();
};
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "incrementRefCount"), 0, incrementRefCount);
}
if (name == "decrementRefCount") {
auto decrementRefCount = JSI_FUNC {
std::lock_guard lock(this->_mutex);

// Decrement our self-counted ref count by one.
_refCount--;
if (_refCount < 1) {
// ARC will then delete the Frame and the underlying Frame Buffer.
this->frame = nil;
}

[frame decrementRefCount];
return jsi::Value::undefined();
};
return jsi::Function::createFromHostFunction(runtime, jsi::PropNameID::forUtf8(runtime, "decrementRefCount"), 0, decrementRefCount);
Expand Down

0 comments on commit e75bb9f

Please sign in to comment.