Skip to content
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

Backup for debuglog in double free issue #14373

Draft
wants to merge 1 commit into
base: cheryllin/firestoreSwiftCpp
Choose a base branch
from
Draft
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
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES"
enableAddressSanitizer = "YES"
enableASanStackUseAfterReturn = "YES">
<MacroExpansion>
<BuildableReference
Expand All @@ -37,6 +38,13 @@
ReferencedContainer = "container:Firestore.xcodeproj">
</BuildableReference>
</MacroExpansion>
<AdditionalOptions>
<AdditionalOption
key = "NSZombieEnabled"
value = "YES"
isEnabled = "YES">
</AdditionalOption>
</AdditionalOptions>
<Testables>
<TestableReference
skipped = "NO">
Expand Down
20 changes: 12 additions & 8 deletions Firestore/Source/API/FIRCallbackWrapper.mm
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#import "FIRCallbackWrapper.h"

#include <iostream>
#include <memory>
#include <utility>
#include <vector>
Expand All @@ -35,21 +36,24 @@
@implementation FIRCallbackWrapper

+ (PipelineSnapshotListener)wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore
completion:(void (^)(PipelineResultVector result,
completion:(void (^)(CppPipelineResult *result,
NSError *_Nullable error))completion {
class Converter : public EventListener<std::vector<PipelineResult>> {
class Converter : public EventListener<CppPipelineResult> {
public:
explicit Converter(std::shared_ptr<api::Firestore> firestore, PipelineBlock completion)
: firestore_(firestore), completion_(completion) {
}

void OnEvent(StatusOr<std::vector<PipelineResult>> maybe_snapshot) override {
void OnEvent(StatusOr<CppPipelineResult> maybe_snapshot) override {
if (maybe_snapshot.ok()) {
completion_(
std::initializer_list<PipelineResult>{PipelineResult::GetTestResult(firestore_)},
nullptr);
std::cout << "zzyzx OnEvent 1" << std::endl;
CppPipelineResult result = maybe_snapshot.ValueOrDie();
std::cout << "zzyzx OnEvent 2 result.id=" << result.id_ << std::endl;
completion_(&result, nullptr);
std::cout << "zzyzx OnEvent 3 result.id=" << result.id_ << std::endl;
} else {
completion_(std::initializer_list<PipelineResult>{}, MakeNSError(maybe_snapshot.status()));
// completion_(PipelineResult::GetTestResult(firestore_),
// MakeNSError(maybe_snapshot.status()));
}
}

Expand All @@ -58,7 +62,7 @@ void OnEvent(StatusOr<std::vector<PipelineResult>> maybe_snapshot) override {
PipelineBlock completion_;
};

return absl::make_unique<Converter>(firestore, completion);
return std::make_shared<Converter>(firestore, completion);
}

@end
10 changes: 5 additions & 5 deletions Firestore/Source/Public/FirebaseFirestore/FIRCallbackWrapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

#import <Foundation/Foundation.h>
#include <memory>

Check failure on line 18 in Firestore/Source/Public/FirebaseFirestore/FIRCallbackWrapper.h

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

'memory' file not found

Check failure on line 18 in Firestore/Source/Public/FirebaseFirestore/FIRCallbackWrapper.h

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

'memory' file not found

Check failure on line 18 in Firestore/Source/Public/FirebaseFirestore/FIRCallbackWrapper.h

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

'memory' file not found

Check failure on line 18 in Firestore/Source/Public/FirebaseFirestore/FIRCallbackWrapper.h

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

'memory' file not found
#include <vector>

namespace firebase {
Expand All @@ -38,10 +38,10 @@

NS_ASSUME_NONNULL_BEGIN

typedef void (^PipelineBlock)(std::vector<api::PipelineResult> result, NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");
typedef api::PipelineResult CppPipelineResult;

typedef std::vector<api::PipelineResult> PipelineResultVector;
typedef void (^PipelineBlock)(CppPipelineResult *result, NSError *_Nullable error)
NS_SWIFT_UNAVAILABLE("Use Swift's closure syntax instead.");

NS_SWIFT_SENDABLE
NS_SWIFT_NAME(CallbackWrapper)
Expand All @@ -51,9 +51,9 @@
// are invoked on a different thread than the one they were originally defined in. If this callback
// is expected to be called on a different thread, it should be marked as `Sendable` to ensure
// thread safety.
+ (std::shared_ptr<core::EventListener<std::vector<api::PipelineResult>>>)
+ (std::shared_ptr<core::EventListener<api::PipelineResult>>)
wrapPipelineCallback:(std::shared_ptr<api::Firestore>)firestore
completion:(void (^NS_SWIFT_SENDABLE)(PipelineResultVector result,
completion:(void (^NS_SWIFT_SENDABLE)(CppPipelineResult *result,
NSError *_Nullable error))completion
NS_SWIFT_NAME(wrapPipelineCallback(firestore:completion:));

Expand Down
6 changes: 4 additions & 2 deletions Firestore/Swift/Source/SwiftAPI/Pipeline.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,17 @@
}

@discardableResult
public func GetPipelineResult() async throws -> [PipelineResult] {
public func GetPipelineResult() async throws -> Int {
return try await withCheckedThrowingContinuation { continuation in
let listener = CallbackWrapper.wrapPipelineCallback(firestore: cppObj.GetFirestore()) {

Check failure on line 34 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / client-app-spm (iOS, ClientApp)

cannot find 'CallbackWrapper' in scope

Check failure on line 34 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / platforms (catalyst)

cannot find 'CallbackWrapper' in scope

Check failure on line 34 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / platforms (macOS)

cannot find 'CallbackWrapper' in scope

Check failure on line 34 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-15, Xcode_16, spm)

cannot find 'CallbackWrapper' in scope

Check failure on line 34 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-15, Xcode_16, spm)

cannot find 'CallbackWrapper' in scope
result, error in
if let error {
continuation.resume(throwing: error)
} else {
// Our callbacks guarantee that we either return an error or a progress event.
continuation.resume(returning: PipelineResult.convertToArrayFromCppVector(result))
print("zzyzx Swift use id: \(result.pointee.id_)")
PipelineResult(result.pointee)

Check warning on line 41 in Firestore/Swift/Source/SwiftAPI/Pipeline.swift

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

result of 'PipelineResult' initializer is unused
continuation.resume(returning: 5)
}
}
cppObj.GetPipelineResult(listener)
Expand Down
11 changes: 6 additions & 5 deletions Firestore/Swift/Source/SwiftAPI/PipelineResult.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,18 @@

public init(_ cppSource: firebase.firestore.api.PipelineResult) {
cppObj = cppSource
print("zzyzx SwiftObj id: \(cppObj.id_)")
}

static func convertToArrayFromCppVector(_ vector: PipelineResultVector)
static func convertToArrayFromCppVector(_ vector: CppPipelineResult)

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / client-app-spm (iOS, ClientApp)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / client-app-spm (iOS, ClientApp)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / client-app-spm (iOS, ClientApp)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / platforms (catalyst)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / platforms (catalyst)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / platforms (catalyst)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / platforms (macOS)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / platforms (macOS)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / platforms (macOS)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / iOS-Device (macos-15, Xcode_16)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / iOS-Device (macos-15, Xcode_16)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-14, Xcode_15.3, spm)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-14, Xcode_15.3, spm)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-14, Xcode_15.3, spm)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-15, Xcode_16, spm)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-15, Xcode_16, spm)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-15, Xcode_16, spm)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-15, Xcode_16, spm)

cannot find type 'CppPipelineResult' in scope

Check failure on line 28 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / swift-build-run (macos-15, Xcode_16, spm)

cannot find type 'CppPipelineResult' in scope
-> [PipelineResult] {
// Create a Swift array and populate it by iterating over the C++ vector
var swiftArray: [PipelineResult] = []

Check warning on line 31 in Firestore/Swift/Source/SwiftAPI/PipelineResult.swift

View workflow job for this annotation

GitHub Actions / client-app-spm-source-firestore (iOS, ClientApp)

variable 'swiftArray' was never mutated; consider changing to 'let' constant

for index in vector.indices {
let cppResult = vector[index]
swiftArray.append(PipelineResult(cppResult))
}
// for index in vector.indices {
// let cppResult = vector[index]
// swiftArray.append(PipelineResult(cppResult))
// }

return swiftArray
}
Expand Down
2 changes: 1 addition & 1 deletion Firestore/Swift/Tests/Integration/PipelineTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,6 @@ final class PipelineTests: FSTIntegrationTestCase {
func testCreatePipeline() async throws {
let pipelineSource: PipelineSource = db.pipeline()
let pipeline: Pipeline = pipelineSource.GetCollection("path")
let _: [PipelineResult] = try await pipeline.GetPipelineResult()
let _ = try await pipeline.GetPipelineResult()
}
}
6 changes: 4 additions & 2 deletions Firestore/core/interfaceForSwift/api/pipeline.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,10 @@ void Pipeline::GetPipelineResult(PipelineSnapshotListener callback) const {
/*include_document_metadata_changes=*/true,
/*wait_for_sync_when_online=*/true);

callback->OnEvent(StatusOr<std::vector<PipelineResult>>(
{(PipelineResult::GetTestResult(firestore_))}));
PipelineResult sample = PipelineResult::GetTestResult(firestore_);

StatusOr<PipelineResult> res(sample);
callback->OnEvent(res);

// class ListenOnce : public EventListener<std::vector<PipelineResult>> {
// public:
Expand Down
2 changes: 1 addition & 1 deletion Firestore/core/interfaceForSwift/api/pipeline.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Firestore;
class PipelineResult;

using PipelineSnapshotListener =
std::shared_ptr<core::EventListener<std::vector<PipelineResult>>>;
std::shared_ptr<core::EventListener<PipelineResult>>;

class Pipeline {
public:
Expand Down
11 changes: 9 additions & 2 deletions Firestore/core/interfaceForSwift/api/pipeline_result.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,29 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "Firestore/core/interfaceForSwift/api/pipeline_result.h"
#include <iostream>

#include "Firestore/core/include/firebase/firestore/timestamp.h"
#include "Firestore/core/interfaceForSwift/api/pipeline_result.h"

namespace firebase {
namespace firestore {

namespace api {

std::atomic<int> next_id(0);

PipelineResult::PipelineResult(std::shared_ptr<Firestore> firestore,
std::shared_ptr<Timestamp> execution_time,
std::shared_ptr<Timestamp> update_time,
std::shared_ptr<Timestamp> create_time)
: firestore_(firestore),
: id_(next_id.fetch_add(1)),
firestore_(firestore),
execution_time_(execution_time),
update_time_(update_time),
create_time_(create_time) {
std::cout << "zzyzx PipelineResult[" << id_ << "]@"
<< reinterpret_cast<std::uintptr_t>(this) << "()" << std::endl;
}

PipelineResult PipelineResult::GetTestResult(
Expand Down
43 changes: 42 additions & 1 deletion Firestore/core/interfaceForSwift/api/pipeline_result.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
#ifndef FIRESTORE_CORE_INTERFACEFORSWIFT_API_PIPELINE_RESULT_H_
#define FIRESTORE_CORE_INTERFACEFORSWIFT_API_PIPELINE_RESULT_H_

#include <atomic>
#include <iostream>
#include <memory>

namespace firebase {
Expand All @@ -28,16 +30,55 @@ namespace api {
class Firestore;
class DocumentReference;

extern std::atomic<int> next_id;

class PipelineResult {
public:
PipelineResult(std::shared_ptr<Firestore> firestore,
std::shared_ptr<Timestamp> execution_time,
std::shared_ptr<Timestamp> update_time,
std::shared_ptr<Timestamp> create_time);

// Copy constructor
PipelineResult(const PipelineResult& other)
: id_(next_id.fetch_add(1)),
firestore_(other.firestore_),
execution_time_(other.execution_time_),
update_time_(other.update_time_),
create_time_(other.create_time_) {
std::cout << "zzyzx PipelineResult[" << id_ << "]@"
<< reinterpret_cast<std::uintptr_t>(this)
<< "(const PipelineResult&) other.id=" << other.id_ << std::endl;
long n = execution_time_.use_count();
std::cout << "Calling copy ctor when refer count is:" << n << std::endl;
}

// Copy assignment operator
PipelineResult& operator=(const PipelineResult& other) {
std::cout << "zzyzx PipelineResult[" << id_ << "]@"
<< reinterpret_cast<std::uintptr_t>(this)
<< ".operator=(const PipelineResult&) other.id_=" << other.id_
<< std::endl;
if (this != &other) {
firestore_ = other.firestore_;
execution_time_ = other.execution_time_;
update_time_ = other.update_time_;
create_time_ = other.create_time_;
}
return *this;
}

static PipelineResult GetTestResult(std::shared_ptr<Firestore> firestore);

private:
~PipelineResult() {
std::cout << "zzyzx PipelineResult[" << id_ << "]@"
<< reinterpret_cast<std::uintptr_t>(this) << "~PipelineResult()"
<< std::endl;
long n = execution_time_.use_count();
std::cout << "Calling destructor when refer count is:" << n << std::endl;
}

int id_;
std::shared_ptr<Firestore> firestore_;
std::shared_ptr<Timestamp> execution_time_;
std::shared_ptr<Timestamp> update_time_;
Expand Down
Loading