3434
3535@implementation FIRCallbackWrapper
3636
37+ // In public Swift documentation for integrating Swift and C++, using raw pointers in C++ is
38+ // generally considered unsafe. However, during an experiment where the result was passed as a value
39+ // instead of a pointer, a double free error occurred. This issue could not be traced effectively
40+ // because the implementation resides within the Swift-C++ transition layer. In this specific use
41+ // case, the C++ OnEvent() scope is destroyed after the Swift callback has been destroyed. Due to
42+ // this ordering, using a raw pointer is a safe workaround for now.
3743+ (PipelineSnapshotListener)wrapPipelineCallback : (std::shared_ptr<api::Firestore>)firestore
38- completion : (void (^)(PipelineResultVector result,
44+ completion : (void (^)(CppPipelineResult *_Nullable result,
3945 NSError *_Nullable error))completion {
40- class Converter : public EventListener <std::vector<PipelineResult> > {
46+ class Converter : public EventListener <CppPipelineResult > {
4147 public:
4248 explicit Converter (std::shared_ptr<api::Firestore> firestore, PipelineBlock completion)
4349 : firestore_(firestore), completion_(completion) {
4450 }
4551
46- void OnEvent (StatusOr<std::vector<PipelineResult> > maybe_snapshot) override {
52+ void OnEvent (StatusOr<CppPipelineResult > maybe_snapshot) override {
4753 if (maybe_snapshot.ok ()) {
48- completion_ (
49- std::initializer_list<PipelineResult>{PipelineResult::GetTestResult (firestore_)},
50- nullptr );
54+ completion_ (&maybe_snapshot.ValueOrDie (), nullptr );
5155 } else {
52- completion_ (std::initializer_list<PipelineResult>{} , MakeNSError (maybe_snapshot.status ()));
56+ completion_ (nullptr , MakeNSError (maybe_snapshot.status ()));
5357 }
5458 }
5559
@@ -58,7 +62,7 @@ void OnEvent(StatusOr<std::vector<PipelineResult>> maybe_snapshot) override {
5862 PipelineBlock completion_;
5963 };
6064
61- return absl::make_unique <Converter>(firestore, completion);
65+ return std::make_shared <Converter>(firestore, completion);
6266}
6367
6468@end
0 commit comments