Skip to content

Commit a0be15b

Browse files
committed
Fix iOS screenshot API to use dispatch_sync and proper memory management
The initial implementation used dispatch_async which caused a NullPointerException because Java method calls require proper thread context. When using dispatch_async, the calling thread's context is not maintained in the async block. Changes: - Changed dispatch_async to dispatch_sync to maintain thread context for Java calls - Added POOL_BEGIN() at the start of the dispatch block for proper autorelease pool - Added POOL_END() before early returns and at the end of the block This follows the same pattern used throughout the codebase for operations that capture images and call back to Java (e.g., createPeerImage at line 2076).
1 parent 5c35e42 commit a0be15b

File tree

1 file changed

+5
-1
lines changed

1 file changed

+5
-1
lines changed

Ports/iOSPort/nativeSources/IOSNative.m

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5212,15 +5212,18 @@ void com_codename1_impl_ios_IOSNative_updatePersonWithRecordID___int_com_codenam
52125212
}
52135213

52145214
void com_codename1_impl_ios_IOSNative_screenshot__(CN1_THREAD_STATE_MULTI_ARG JAVA_OBJECT instanceObject) {
5215-
dispatch_async(dispatch_get_main_queue(), ^{
5215+
dispatch_sync(dispatch_get_main_queue(), ^{
5216+
POOL_BEGIN();
52165217
UIView *view = [CodenameOne_GLViewController instance].view;
52175218
UIImage *img = cn1_captureView(view);
52185219
if (!img) {
5220+
POOL_END();
52195221
return;
52205222
}
52215223

52225224
NSData *png = UIImagePNGRepresentation(img);
52235225
if (!png) {
5226+
POOL_END();
52245227
return;
52255228
}
52265229

@@ -5231,6 +5234,7 @@ void com_codename1_impl_ios_IOSNative_screenshot__(CN1_THREAD_STATE_MULTI_ARG JA
52315234
memcpy((JAVA_ARRAY_BYTE*)((JAVA_ARRAY)byteArr)->data, (const jbyte*)[png bytes], len);
52325235

52335236
com_codename1_impl_ios_IOSImplementation_onScreenshot___byte_1ARRAY(CN1_THREAD_GET_STATE_PASS_ARG byteArr);
5237+
POOL_END();
52345238
});
52355239
}
52365240

0 commit comments

Comments
 (0)