-
Notifications
You must be signed in to change notification settings - Fork 67
CGImageRef gets released before the listener is called #1467
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
https://stackoverflow.com/questions/20530342/correct-way-to-handle-cgimageref-in-containers-under-arc this seems to be relevant here (and probably for other CF types) |
Looks like @kekland The workaround you've written there is probably the best approach. The only improvement I'd make is to try pulling |
@liamappelbe thanks for the reply, will do that. About handling non-objc pointers with ARC - it seems like there's methods I think it's possible to use I will probably take a deeper look into this today, will post my findings here. |
Hmmm. I'm a bit confused by the fact that |
CGImageRef is defined as the following: // CoreGraphics/CGImage.h:12
typedef struct CF_BRIDGED_TYPE(id) CGImage *CGImageRef; /* Swift Sendable */ CFTypeRef: // CoreFoundation/CFBase.h:497
typedef const CF_BRIDGED_TYPE(id) void * CFTypeRef; I think it should be possible. Calling Reading through https://developer.apple.com/library/archive/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/Concepts/Ownership.html - it seems that the safe approach would be to not pass it over to ARC (as by their definition if you don't create the CF object via a method called I'm not sure whether that would be in the scope of the ffigen/obj-c package, but it would probably make sense that this is also automatically managed |
Sounds good. The only question is how to detect these types from libclang |
This is where I have absolutely zero knowledge :P My only idea was to hardcode a list of CF types. All CF types have a method https://developer.apple.com/documentation/corefoundation/cftypeid
I wrote a Python script to go through the headers in import pathlib
headers_location = pathlib.Path('/Library/Developer/CommandLineTools/SDKs/MacOSX.sdk/System/Library/Frameworks/')
# recursively find all headers in the SDK
headers = list(headers_location.glob('**/*.h'))
cfTypesList = []
for header in headers:
try:
with open(header, 'r', encoding='latin-1') as f:
lines = f.readlines()
for line in lines:
# If line has `CFTypeID {x}GetTypeID(void)`
if 'CFTypeID' in line and 'GetTypeID' in line and 'void' in line:
# print(header)
# print(line)
# Extract the type name
tokens = line.split()
# Extract the type name
type_name = tokens[tokens.index('CFTypeID') + 1].split('GetTypeID')[0]
cfTypesList.append(type_name)
except Exception as e:
print('Error reading file:', header)
print(e)
continue
print(cfTypesList) which results in the following:
When generating the listeners, we can check if the type of the reference matches one of those types in the list, and in that case use the CF retain/release mechanisms |
Uh oh!
There was an error while loading. Please reload this page.
Hi!
I'm trying to use the
AVAssetImageGenerator.generateCGImagesAsynchronouslyForTimes_completionHandler
via ffigen to extract video frames (e.g. the thumbnail). For some reason, when the completion handler is called, in some situations (not always!) it seems like the CGImageRef is already released when the listener is called. Here's a shortened piece of code that shows the usage from Dart:From what I can understand, somehow the CGImageRef is getting released too early, before the listener is called. I was able to consistently replicate this on my phone. Also, I was able to fix this in the generated code by adding an explicit
CGImageRetain
call to the listener block:and then releasing the CGImage on the Dart side. This seems to fix the issue, but I'm not sure whether it should be that way.
If needed, I can provide an example project which should be able to reproduce this issue. I'm using ffigen/objc from #1463
The text was updated successfully, but these errors were encountered: