Skip to content

C++ interop pointer double freed when passing std::vector<float> to Swift initializer #69380

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

Closed
efroemling opened this issue Oct 24, 2023 · 2 comments
Assignees
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ swift 5.10 triage needed This issue needs more specific labels

Comments

@efroemling
Copy link

efroemling commented Oct 24, 2023

Description
If I define a Swift class that accepts a std::vector<float> as the argument in its initializer, I can successfully create an instance of that class from C++, but when that object is destructed I get the following error: *** error for object 0x600001298050: pointer being freed was not allocated.

Steps to reproduce
The following steps reproduce the crash for me using XCode 15.0.1 (15A507) on an M1 Max MBP running Sonoma 14.1:

  • Create a new ‘Command Line Tool’ project named “TestCommand”.
  • Create a new C++ file named ‘test’ (with header). Click ‘Create Bridging Header’ when it asks.
  • In TestCommand-Bridging-Header.h, add:
    #include "test.hpp"
  • In test.hpp, add:
    #include <vector>
    
    // Let Swift know about our test call and the type we're gonna send it.
    using CppFloatVec = std::vector<float>;
    void doTest();
  • In test.cpp, add:
    #include <TestCommand-Swift.h>
    
    void doTest() {
      // Create an instance of our Swift TestClass
      // (which will then be torn down since it goes out of scope immediately).
      auto vals = std::vector<float>{1.0f};
      auto obj = TestCommand::TestClass::init(vals);
    }
  • In main.swift, add:
    public class TestClass {
    
      public init(_ values: CppFloatVec) {
        for v in values {
          print("GOT VALUE", v)
        }
        print("DONE PRINTING.")
      }
    
      static public func create(_ values: CppFloatVec) -> TestClass {
        return TestClass(values)
      }
    }
    
    // Call our C++ test code.
    doTest()
  • Under build settings for the TestCommand target, set C++ and Objective-C Interoperability to “C++/Objective C++”
  • You should now be able to build and run the project, which should give the following output and then break on the debugger in std::vector's destructor due to a double free.
    Hello, World!
    GOT VALUE 1.0
    DONE PRINTING.
    TestCommand(39408,0x1e1271ec0) malloc: *** error for object 0x600002c70050: pointer being freed was not allocated
    TestCommand(39408,0x1e1271ec0) malloc: *** set a breakpoint in malloc_error_break to debug
    
  • Interestingly, changing the line from auto obj = TestCommand::TestClass::init(vals); to auto obj = TestCommand::TestClass::create(vals); avoids the error.

Environment

  • Swift compiler version info: swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1)

  • Xcode version info: Xcode 15.0.1 Build version 15A507

  • Deployment target: macOS Sonoma 14.1 (23B73)

@efroemling efroemling added bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. triage needed This issue needs more specific labels labels Oct 24, 2023
@hyp hyp added the c++ interop Feature: Interoperability with C++ label Oct 24, 2023
@hyp hyp self-assigned this Oct 26, 2023
@hyp
Copy link
Contributor

hyp commented Oct 26, 2023

Problem with @owned convention here as well I think.

@hyp hyp added the swift 5.10 label Oct 26, 2023
@hyp
Copy link
Contributor

hyp commented Oct 27, 2023

This is a duplicate of #69372, just a different manifestation of it (different type)

@hyp hyp closed this as completed Oct 27, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. c++ interop Feature: Interoperability with C++ swift 5.10 triage needed This issue needs more specific labels
Projects
None yet
Development

No branches or pull requests

2 participants