-
Notifications
You must be signed in to change notification settings - Fork 10.4k
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
shared_ptr object creation is not working between swift/c++ interoperability. #67355
Comments
|
Thanks.. It is working fine for me now. Just a small question in addition to this: |
I'm chiming in into this issue, because there is something that I might not completely understand about the new swift<->c++ interop: although sharing std::shared_ptr (like in this example) or std::vector works, I find it still quite underwhelming and not really "interop" material, since all you can do is pass around shared_ptrs or vectors by value, that are necessarily owned or created by their C++ counterpart. Despite what the swift doc is saying (and that one is really good for once, so much that it might be overselling the feature at that point...).
|
Sorry to hear that @moretromain , let me try to respond to specific points:
You should be able to access fields and call the specific methods on the pointee, for instance:
The ergonomics for shared_ptr are not great at the moment, and we're going to be working on improving them. We will need some Swift language additions as well, specifically easier ways to borrow values: https://forums.swift.org/t/desired-swift-language-features-that-can-advance-the-state-of-c-interoperability-support/66144/9. We are planning to work on it though!
It sounds like you're hitting #67410 . We're investigating it right now. You should be able to work around it by manually conforming a specific vector type you need to Once it's conformed you should be able to convert it to array by just going
Could you file an issue that describes the CMake examples build errors you're seeing to https://github.com/apple/swift-cmake-examples and CC @etcwilde on it.
|
@moretromain, yes, please do let me know what you're seeing, what Swift version you have, and what CMake version. Thanks! |
@redcode It looks like it works as expected at least as per the current model. Note that
This is not different from what happens in this case:
|
The problem is that the object, in my example, is not trivially destructible. The copy of |
yes, while my explanation was to reason about the behavior within the current semantics/model, there is certainly a lot of room to improve support for C++ smart pointers in Swift e.g. as highlighted here: https://forums.swift.org/t/desired-swift-language-features-that-can-advance-the-state-of-c-interoperability-support/66144/9 Also, since CppObject is actually a type with shared storage, I think it is quite reasonable to import it as a Swift class with SWIFT_IMMORTAL_REFERENCE annotation, as mentioned. API Notes can come in handy for adding these annotations to a type defined in an external library. |
This is very interesting, I was not aware of this new Clang feature. It seems to be just what I need to solve a number of problems. Thanks :) |
I found two very interesting "issues" with
|
Description
Creating a shared_ptr object in the swift file is not working. It is stated in the documentation that shared_ptr functionality is supported but it is not working.
C++ header file: Sample.h
#ifndef Sample_h
#define Sample_h
#include
#include
inline std::shared_ptr makeSharedData(int data) {
return std::make_shared (data);
}
class sample {
public:
sample(std::shared_ptr data);
int read_data();
void write_data(int data);
void ref_func(int& data);
private:
std::shared_ptr m_data;
};
#endif /* Sample_h */
Respective Sample.cpp file :
#include "Sample.h"
sample::sample(std::shared_ptr data) {
m_data = data;
}
int sample::read_data() {
return *m_data;
}
void sample::write_data(int data) {
*m_data = data;
}
void sample::ref_func(int& data) {
data = 100;
}
Swift File Contents : TestFile.swift
import Foundation
func connectCPP() -> Int32 {
//1. making use of the inline function in header file and calling that to get the shared_ptr object which in turns used to make the
// class object. This is throwing below error :
/* std::__1::__shared_ptr_emplace<int, std::__1::allocator>::
__shared_ptr_emplace(), referenced from:__shared_ptr_emplace(), referenced from:vtable for std::__1::__shared_ptr_emplace<int, std::__1::allocator> in TestFile.o
std::__1::__shared_ptr_emplace<int, std::__1::allocator>::
vtable for std::__1::__shared_ptr_emplace<int, std::__1::allocator> in TestFile.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
*/
var obj = sample(makeSharedData(30))
//2. If I am trying to use make_shared directly in swift file, It is throwing error of symbol not found for make_shared and
// shared_ptr.
}
If shared_ptr support is present in current version of interoperability, then it should be working fine without any error.
The text was updated successfully, but these errors were encountered: