Skip to content

Conversation

parkera
Copy link
Contributor

@parkera parkera commented Mar 17, 2025

The extension on String to provide a C string works well enough on Darwin, when used with an autoreleasepool { }. However, on other platforms where we have no autorelease pools, it simply leaks. Even though this API is useful on Darwin, we should deprecate it in swift-corelibs-foundation because it has repeatedly been the source of some confusing behavior.

#3885

Unverified

The signing certificate or its chain could not be verified.
@@ -632,6 +632,7 @@ extension StringProtocol {

/// Returns a representation of the string as a C string
/// using a given encoding.
@available(*, deprecated, message: "On platforms without Objective-C autorelease pools, use withCString instead")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's an NSString.cString(using:) that has the same issue, FWIW

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that we should refer people to use String.withCString(encodedAs:_) instead of this message.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure. There is one place we use this API in keyed archiver that will have to get a follow-up.

Unverified

The signing certificate or its chain could not be verified.
@parkera
Copy link
Contributor Author

parkera commented Mar 17, 2025

@swift-ci test

1 similar comment
@parkera
Copy link
Contributor Author

parkera commented Mar 18, 2025

@swift-ci test

@@ -632,6 +632,7 @@ extension StringProtocol {

/// Returns a representation of the string as a C string
/// using a given encoding.
@available(*, deprecated, message: "On platforms without Objective-C autorelease pools, use withCString(encodedAs:_) instead")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is actually safe to use though, right?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not unless we reimplement it to use a lifetime-limited version of the NSString cString(using:) function. Which I suppose we could do.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's worth it, but it might mean less churn.

Unverified

The signing certificate or its chain could not be verified.
@parkera
Copy link
Contributor Author

parkera commented Mar 18, 2025

@swift-ci test

@parkera
Copy link
Contributor Author

parkera commented Mar 19, 2025

@swift-ci test windows

@parkera parkera merged commit cb4ee87 into swiftlang:main Mar 24, 2025
2 checks passed
@parkera parkera deleted the parkera/deprecate_cString branch March 24, 2025 16:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants