migueldeicaza / SwiftGodot

New Godot bindings for Swift
https://migueldeicaza.github.io/SwiftGodotDocs/tutorials/swiftgodot-tutorials/
MIT License
1.18k stars 77 forks source link

A bug when `Object` is inline-wrapped into a `Variant`. #574

Open elijah-semyonov opened 1 month ago

elijah-semyonov commented 1 month ago

This test fails:

func testObjectArrayAcceptsNil() {
    let array = GArray(Object.self)
    array.append(Variant())
    array.append(Variant())

    XCTAssertEqual(array.count, 2)
    XCTAssertEqual(array[0], Variant())
    XCTAssertEqual(array[1], Variant())

    array.append(Variant(Object()))
    XCTAssertEqual(array.count, 3)
  }

Console prints:

Attempted to push_back an invalid (previously freed?) object instance into a 'TypedArray.

This crashes:

  func testObjectNotLost() {
      let variant = Variant(Object())
      let methodListVariant: Variant

      switch variant.call(method: "get_method_list") {
      case .success(let variant):
          methodListVariant = variant
      case .failure(let error):
          XCTFail("Could not get method list, error: \(error)")
          return
      }

      guard let methodList = GArray(methodListVariant) else {
          XCTFail("Could not get method list, got \(methodListVariant.gtype) instead")
          return
      }

      XCTAssertFalse(methodList.isEmpty())
 }

Note: Only framework types are affected RefCounted-inherited types are not affected