Skip to content

Encountering "Constant strings cannot be deallocated" in Linux foundation #4642

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

Closed
dhoepfl opened this issue Oct 10, 2022 · 2 comments
Closed
Assignees

Comments

@dhoepfl
Copy link
Contributor

dhoepfl commented Oct 10, 2022

Describe the bug
Using Bundle.module.url(forResource:, withExtension:,subdirectory:) results in error message "Foundation/NSCFString.swift:119: Fatal error: Constant strings cannot be deallocated"

Steps To Reproduce
Steps to reproduce the behavior:

  1. Create a new package mkdir swiftbug; cd swiftbug; swift package init
  2. In Package.swift, add a resource to the testTarget:
        .testTarget(
            name: "swiftbugTests",
            dependencies: ["swiftbug"],
            resources: [
               .copy("Resources")
            ]),
  1. Create a resource: mkdir Tests/swiftbugTests/Resources; touch Tests/swiftbugTests/Resources/dummy.png
  2. Modify the test in Tests/swiftbugTests/swiftbugTests.swift to match:
    func testExample() async throws {
        // This is an example of a functional test case.
        // Use XCTAssert and related functions to verify your tests produce the correct
        // results.
        XCTAssertEqual(swiftbug().text, "Hello, World!")

        _ = Bundle.module.url(forResource: "filename", withExtension: nil, subdirectory: "Resources")
    }
  1. Run the test: swift test

Expected behavior
Tests should finish without an error.

Actual result

Building for debugging...
[20/20] Linking swiftbugPackageTests.xctest
Build complete! (4.82s)
Test Suite 'All tests' started at 2022-10-10 17:57:06.166
Test Suite 'debug.xctest' started at 2022-10-10 17:57:06.171
Test Suite 'swiftbugTests' started at 2022-10-10 17:57:06.171
Test Case 'swiftbugTests.testExample' started at 2022-10-10 17:57:06.172
Foundation/NSCFString.swift:119: Fatal error: Constant strings cannot be deallocated
error: Exited with signal code 4

Environment (please fill out the following information)

  • OS: Debian Linux
  • Self compiled swiftc: LLVM c205842748e1789, Swift 1f8a5d6045262bb

Additional context
Might be related to/regression of #3785

@valerianb
Copy link

FYI, reported it back in 2021 swiftlang/swift#56730

dhoepfl added a commit to dhoepfl/swift-corelibs-foundation that referenced this issue Nov 3, 2022
…deallocated" in Linux foundation

The reported error was an CFSTR("") which is later released. While this should
not be a problem, the Linux implementation of CFSTR does not ignore dealloc
on constant strings. Fixed this by calling CFRetain on the constant string.

Strictly speaking this is only a workaround. Issue swiftlang#1351 has some hints how
this could be fixed but the workaround is used a over the code so I think it
is okay to use it here, too.

I found the same problem in CFDateIntervalFormatter.c where it appeared in a
error handling code path that should never be called. Fixed anyways.
parkera pushed a commit that referenced this issue Nov 2, 2023
…ed" in Linux foundation (#4653)

The reported error was an CFSTR("") which is later released. While this should
not be a problem, the Linux implementation of CFSTR does not ignore dealloc
on constant strings. Fixed this by calling CFRetain on the constant string.

Strictly speaking this is only a workaround. Issue #1351 has some hints how
this could be fixed but the workaround is used a over the code so I think it
is okay to use it here, too.

I found the same problem in CFDateIntervalFormatter.c where it appeared in a
error handling code path that should never be called. Fixed anyways.
@dhoepfl
Copy link
Contributor Author

dhoepfl commented Nov 3, 2023

On this day, one year ago, I submitted the pull request.
A few hours ago, it was merged.

This should resolves a crashing bug initially reported 2177 days (nearly 6 years) ago.

🎉

@dhoepfl dhoepfl closed this as completed Nov 3, 2023
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

No branches or pull requests

3 participants