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-5280] Foundation: FileHandle.swift: memory leak in _readDataOfLength #4285
Comments
Comment by Denis S (JIRA) This bug is especially important when you read from an infinite communication channel like tty, it eats all the memory. |
@swift-ci create |
Comment by Denis S (JIRA) Hi! Any update on this? I've just hit it one more time. File reading is a basic operation in OS, it should be safe. |
Comment by Andrew Wagner (JIRA) I am using the following workaround for now: extension FileHandle {
public func safelyReadData(ofLength length: Int) -> Data {
#if os(Linux)
var leakingData = self.readData(ofLength: length)
var data: Data = Data()
if leakingData.count > 0 {
leakingData.withUnsafeMutableBytes({ (bytes: UnsafeMutablePointer<UInt8>) -> Void in
data = Data(bytesNoCopy: bytes, count: leakingData.count, deallocator: .free)
})
}
return data
#else
return self.readData(ofLength: length)
#endif
}
} On Linux it is creating a copy of the data that will actually free the underlying data when deallocated. |
Comment by Denis S (JIRA) Yes, I did similar extension as you with ".free" deallocator. Thanks to Swift's Extension mechanism that allows us to plug leaks and bugs as soon as possible. |
Comment by Matt Rajca (JIRA) |
Environment
Ubuntu 16.04
Additional Detail from JIRA
md5: 6496dc2585e2a226ba7a13c8978d8c65
Issue Description:
internal func readDataOfLength( length: Int, untilEOF: Bool) -> Data
Leaks memory when returns data.
I suppose it should be:
On Apple platforms it doesn't leak because Foundation returns autoreleasing NSData, it automatically gets added to RunLoop's autorelease pool. Or if you have your own local autorelease pool to destroy object earlier.
The text was updated successfully, but these errors were encountered: