diff --git a/Source/AwsCommonRuntimeKit/auth/credentials/CredentialsProvider.swift b/Source/AwsCommonRuntimeKit/auth/credentials/CredentialsProvider.swift index dc3c6b98b..7a0f67b5c 100644 --- a/Source/AwsCommonRuntimeKit/auth/credentials/CredentialsProvider.swift +++ b/Source/AwsCommonRuntimeKit/auth/credentials/CredentialsProvider.swift @@ -11,7 +11,7 @@ public protocol CredentialsProviding { } /// A pair defining an identity provider and a valid login token sourced from it. -public struct CognitoLoginPair: CStruct { +public class CognitoLoginPair: CStruct { public var IdentityProviderName: String public var IdentityProviderToken: String @@ -20,17 +20,20 @@ public struct CognitoLoginPair: CStruct { self.IdentityProviderName = identityProviderName self.IdentityProviderToken = identityProviderToken } - + + private var name_buffer: aws_byte_buf = aws_byte_buf() + private var token_buffer: aws_byte_buf = aws_byte_buf() typealias RawType = aws_cognito_identity_provider_token_pair func withCStruct(_ body: (aws_cognito_identity_provider_token_pair) -> Result) -> Result { var token_pair = aws_cognito_identity_provider_token_pair() - - return withByteCursorFromStrings(IdentityProviderName, - IdentityProviderToken) { identityProviderNameCursor, IdentityProviderTokenCursor in - token_pair.identity_provider_name = identityProviderNameCursor - token_pair.identity_provider_token = IdentityProviderTokenCursor - return body(token_pair) - } + token_pair.identity_provider_name = aws_byte_cursor_from_buf(&self.name_buffer) + token_pair.identity_provider_token = aws_byte_cursor_from_buf(&self.token_buffer) + return body(token_pair) + } + + deinit{ + aws_byte_buf_clean_up(&self.name_buffer) + aws_byte_buf_clean_up(&self.token_buffer) } } diff --git a/Source/AwsCommonRuntimeKit/crt/CStruct.swift b/Source/AwsCommonRuntimeKit/crt/CStruct.swift index 73862e3a1..315e5c12a 100644 --- a/Source/AwsCommonRuntimeKit/crt/CStruct.swift +++ b/Source/AwsCommonRuntimeKit/crt/CStruct.swift @@ -20,8 +20,8 @@ extension CStruct { } extension Array where Element: CStruct { - /// Convert a CStruct Array into raw c pointer. The function would not do a deep copy of the CStruct element. If you required a deep copy - /// make sure to clean up the memory underneath. + /// Convert a CStruct Array into raw c pointer. However, as the conversion made by withCPointer will be unavailable out of + /// the scope, we need to make sure that we have a copy of the underlying memory. func withAWSArrayList(_ body: (OpaquePointer?) throws -> Result) rethrows -> Result { guard capacity != 0 else { return try body(nil) @@ -42,9 +42,11 @@ extension Array where Element: CStruct { forEach { $0.withCPointer { // `aws_array_list_push_back` will do a memory copy of $0 into array_list, but it would - // not do a deep copy there. + // not do a deep copy there. For example, if the c struct contains a aws_byte_cursor, it + // would only copy the cursor, not the underlying memory it points to, it is possible that + // the memory will be unavailable out of the scope. guard aws_array_list_push_back(array_list, $0) == AWS_OP_SUCCESS else { - fatalError("Unable to add user property") + fatalError("Unable to add array element") } } }