Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/pigeon/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 26.1.2

* [swift] Adds a do/catch when a deallocated object calls back to Dart to prevent a crash.

## 26.1.1

* Updates supported `analyzer` versions to 8.x or 9.x.
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/src/generator_tools.dart
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import 'generator.dart';
/// The current version of pigeon.
///
/// This must match the version in pubspec.yaml.
const String pigeonVersion = '26.1.1';
const String pigeonVersion = '26.1.2';

/// Read all the content from [stdin] to a String.
String readStdin() {
Expand Down
2 changes: 1 addition & 1 deletion packages/pigeon/lib/src/swift/swift_generator.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1997,7 +1997,7 @@ func deepHash${generatorOptions.fileSpecificClassNameComponent}(value: Any?, has
self.api = api
}

public func onDeinit(identifier: Int64) {
public func onDeinit(identifier: Int64) throws {
api.removeStrongReference(identifier: identifier) {
_ in
}
Expand Down
10 changes: 8 additions & 2 deletions packages/pigeon/lib/src/swift/templates.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ String instanceManagerFinalizerDelegateTemplate(InternalSwiftOptions options) =>
/// Handles the callback when an object is deallocated.
protocol ${instanceManagerFinalizerDelegateName(options)}: AnyObject {
/// Invoked when the strong reference of an object is deallocated in an `InstanceManager`.
func onDeinit(identifier: Int64)
func onDeinit(identifier: Int64) throws
}

''';
Expand Down Expand Up @@ -66,7 +66,13 @@ internal final class ${_instanceManagerFinalizerName(options)} {
}

deinit {
delegate?.onDeinit(identifier: identifier)
do {
try delegate?.onDeinit(identifier: identifier)
} catch {
NSLog(
"Failed to call `onDeinit` on object with identifier: \\(identifier)\\n\\(error)\\nStacktrace: \\(Thread.callStackSymbols)"
)
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ private func nilOrValue<T>(_ value: Any?) -> T? {
/// Handles the callback when an object is deallocated.
protocol ProxyApiTestsPigeonInternalFinalizerDelegate: AnyObject {
/// Invoked when the strong reference of an object is deallocated in an `InstanceManager`.
func onDeinit(identifier: Int64)
func onDeinit(identifier: Int64) throws
}

// Attaches to an object to receive a callback when the object is deallocated.
Expand Down Expand Up @@ -113,7 +113,13 @@ internal final class ProxyApiTestsPigeonInternalFinalizer {
}

deinit {
delegate?.onDeinit(identifier: identifier)
do {
try delegate?.onDeinit(identifier: identifier)
} catch {
NSLog(
"Failed to call `onDeinit` on object with identifier: \(identifier)\n\(error)\nStacktrace: \(Thread.callStackSymbols)"
)
}
}
}

Expand Down Expand Up @@ -428,7 +434,7 @@ open class ProxyApiTestsPigeonProxyApiRegistrar {
self.api = api
}

public func onDeinit(identifier: Int64) {
public func onDeinit(identifier: Int64) throws {
api.removeStrongReference(identifier: identifier) {
_ in
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ final class InstanceManagerTests: XCTestCase {
XCTAssertNil(
objc_getAssociatedObject(object!, ProxyApiTestsPigeonInternalFinalizer.associatedObjectKey))
}

func testExceptionInDeinitDoesNotCauseCrash() {
let finalizerDelegate = ThrowingFinalizerDelegate()

var object: NSObject? = NSObject()
ProxyApiTestsPigeonInternalFinalizer.attach(
to: object!, identifier: 0, delegate: finalizerDelegate)

XCTAssertNoThrow(object = nil)
XCTAssertTrue(finalizerDelegate.deinitCalled)
}
}

class EmptyFinalizerDelegate: ProxyApiTestsPigeonInternalFinalizerDelegate {
Expand All @@ -160,3 +171,12 @@ class TestFinalizerDelegate: ProxyApiTestsPigeonInternalFinalizerDelegate {
lastHandledIdentifier = identifier
}
}

class ThrowingFinalizerDelegate: ProxyApiTestsPigeonInternalFinalizerDelegate {
var deinitCalled = false

func onDeinit(identifier: Int64) throws {
deinitCalled = true
fatalError()
}
}
2 changes: 1 addition & 1 deletion packages/pigeon/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: pigeon
description: Code generator tool to make communication between Flutter and the host platform type-safe and easier.
repository: https://github.com/flutter/packages/tree/main/packages/pigeon
issue_tracker: https://github.com/flutter/flutter/issues?q=is%3Aissue+is%3Aopen+label%3A%22p%3A+pigeon%22
version: 26.1.1 # This must match the version in lib/src/generator_tools.dart
version: 26.1.2 # This must match the version in lib/src/generator_tools.dart

environment:
sdk: ^3.8.0
Expand Down