Skip to content

objective_c throws '_isValidObject(ptr)': is not true #1340

Closed
@yanshouwang

Description

@yanshouwang

I have written a method with Swift and use ffigen to generate the dart bindings.

When I call the metod from Dart, the method throws an error like this.

[log] 'package:objective_c/src/internal.dart': Failed assertion: line 113 pos 12: '_isValidObject(ptr)': is not true.
[log] #0      _AssertionError._doThrowNew (dart:core-patch/errors_patch.dart:51:61)
      #1      _AssertionError._throwNew (dart:core-patch/errors_patch.dart:40:5)
      #2      ObjCObjectBase._retain (package:objective_c/src/internal.dart:113:12)
      #3      new _ObjCFinalizable (package:objective_c/src/internal.dart:54:7)
      #4      new ObjCObjectBase (package:objective_c/src/internal.dart)
      #5      new NSObject._ (package:objective_c/src/objective_c_bindings_generated.dart:25:9)
      #6      new NSObject.castFromPointer (package:objective_c/src/objective_c_bindings_generated.dart:34:14)
      #7      new NSString._ (package:objective_c/src/objective_c_bindings_generated.dart:506:15)
      #8      new NSString.castFromPointer (package:objective_c/src/objective_c_bindings_generated.dart:515:14)
      #9      new ObjCBlock_ffiVoid_NSString_NSError.listener.<anonymous closure> (package:camerax_ios/src/ffi.g.dart:37907:58)
      #10     _ObjCBlock_ffiVoid_NSString_NSError_closureTrampoline (package:camerax_ios/src/ffi.g.dart:37825:39)
      #11     new ObjCBlock_ffiVoid_NSString_NSError.listener.<anonymous closure> (package:camerax_ios/src/ffi.g.dart)
      #12     _rootRunUnary (dart:async/zone.dart:1415:13)
      #13     _CustomZone.runUnary (dart:async/zone.dart:1308:19)
      #14     _CustomZone.runUnaryGuarded (dart:async/zone.dart:1217:7)
      #15     _CustomZone.bindUnaryCallbackGuarded.<anonymous closure> (dart:async/zone.dart:1254:26)
      #16     _RawReceivePort._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12)

Here is the Swift method.

    @objc public func takePictureToAlbum(name: String?, completionHandler handler: @escaping (String?, (any Error)?) -> Void) {
        requestPhotoLibraryAuthorization() { [self] status in
            if status == .authorized {
                let settings = AVCapturePhotoSettings(from: capturePhotoSettings)
                let delegate = CameraCapturePhotoCaptureDelegate() { [self] photo, error in
                    if let error {
                        handler(nil, error)
                    } else {
                        savePhoto(photo, name: name, completionHandler: handler)
                    }
                }
                capturePhotoDelegates.append(delegate)
                capturePhotoOutput.capturePhoto(with: settings, delegate: delegate)
            } else {
                handler(nil, CameraError.unauthorized)
            }
        }
    }

Here is the Dart caller.

  @override
  Future<Uri> takePictureToAlbum({String? name}) async {
    final completer = Completer<Uri>();
    final handler =
        ffi.ObjCBlock_ffiVoid_NSString_NSError.listener((savedPath, error) {
      if (error == null) {
        final savedUri = Uri.file('$savedPath');
        completer.complete(savedUri);
      } else {
        completer.completeError(error);
      }
    });
    ffiValue.takePictureToAlbumWithName_completionHandler_(
      name?.toNSString(),
      handler,
    );
    final savedUri = await completer.future;
    return savedUri;
  }

Metadata

Metadata

Assignees

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions