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

[SR-3904] NSCache on Linux only works with static keys #3888

Closed
alblue opened this issue Feb 8, 2017 · 2 comments
Closed

[SR-3904] NSCache on Linux only works with static keys #3888

alblue opened this issue Feb 8, 2017 · 2 comments

Comments

@alblue
Copy link
Contributor

alblue commented Feb 8, 2017

Previous ID SR-3904
Radar None
Original Reporter @alblue
Type Bug
Status Closed
Resolution Done
Environment

Linux (Ubuntu 14.04), Swift 3.0.2 release

Additional Detail from JIRA
Votes 0
Component/s Foundation
Labels Bug, Linux
Assignee naithar (JIRA)
Priority Medium

md5: 4082a5d33afc742e7794d6fbe23ca817

cloned from:

  • SR-3536 Memory leak with String.range(options: .regularExpression)

Issue Description:

NSCache's implementation on Linux only works when the keys are statically defined keys, such as globally defined static constants.

  open class NSCache ... {
    private var _entries = Dictionary<UnsafeRawPointer, NSCacheEntry<KeyType, ObjectType>>()
    open func object(forKey key: KeyType) -> ObjectType? {
        var object: ObjectType?
        
        let keyRef = unsafeBitCast(key, to: UnsafeRawPointer.self)
        
        _lock.lock()
        if let entry = _entries[keyRef] {
            object = entry.value
        }
        _lock.unlock()
        
        return object
    }
  }

As a result, when using dynamically created keys (such as the result of String interpolation) this implementation fails and results in excessive growth; in addition, it does not seem to respect the countLimit. So any Linux code using NSCache directly or indirectly with non-static keys will experience unconstrained memory growth as items are added.

@belkadan
Copy link

belkadan commented Feb 8, 2017

I suspect it was only tested with class-typed keys. :-(

@swift-ci
Copy link
Contributor

swift-ci commented Mar 3, 2017

Comment by Sergey Minakov (JIRA)

Should be fixed in master.
Swift 3.1 PR: 901

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
@shahmishal shahmishal transferred this issue from apple/swift May 5, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants