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-5256] Memory leak with Foundation XMLNode.nodes(forXPath:) #4287

Closed
swift-ci opened this issue Jun 19, 2017 · 3 comments
Closed

[SR-5256] Memory leak with Foundation XMLNode.nodes(forXPath:) #4287

swift-ci opened this issue Jun 19, 2017 · 3 comments

Comments

@swift-ci
Copy link
Contributor

Previous ID SR-5256
Radar None
Original Reporter djones6 (JIRA User)
Type Bug
Status Resolved
Resolution Done
Environment

Ubuntu 14.04, tried with both Swift 3.1.1 + 4.0-2017-06-12a snapshot

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

md5: d135788b67f892d4da8ac6fd1d4c05fc

Issue Description:

The following code leaks on Linux:

import Foundation
let path = FileManager.default.currentDirectoryPath.appending("/sample.xml")
let xml = try! XMLDocument(contentsOf: URL(fileURLWithPath: path), options: [])
let node = try! xml.nodes(forXPath: "/items/item[@id='1']").first!
while true {
    do {
        _ = try node.nodes(forXPath: "@id").first!.stringValue!
    } catch {
        exit(1)
    }
}

Here's the input document:

<?xml version="1.0" encoding="utf-8"?>
<items><item id="1">Some text</item></items>

Running through valgrind indicates that allocations as a result of _CFXMLNodesForXPath -> xmlXPathNewContext -> xmlHashCreate (libxml2) are never freed. Here's what happens if I let the above loop run 200,000 times:

valgrind --tool=massif ./xmlLeak_sr
ms_print massif.out.<pid> | swift-demangle | less
...
    GB
2.484^                                                                       #
     |                                                                     @@#
     |                                                                 ::::@@#
     |                                                             @@::::: @@#
     |                                                         @@@@@@: ::: @@#
     |                                                      @@@@ @@@@: ::: @@#
     |                                                    @@@@@@ @@@@: ::: @@#
     |                                                @@@@@@@@@@ @@@@: ::: @@#
     |                                            ::@@@ @ @@@@@@ @@@@: ::: @@#
     |                                       :::::: @ @ @ @@@@@@ @@@@: ::: @@#
     |                                    @@@: :: : @ @ @ @@@@@@ @@@@: ::: @@#
     |                                 @@@@ @: :: : @ @ @ @@@@@@ @@@@: ::: @@#
     |                             ::::@ @@ @: :: : @ @ @ @@@@@@ @@@@: ::: @@#
     |                         @@@:: ::@ @@ @: :: : @ @ @ @@@@@@ @@@@: ::: @@#
     |                       ::@ @:: ::@ @@ @: :: : @ @ @ @@@@@@ @@@@: ::: @@#
     |                  @@@@@::@ @:: ::@ @@ @: :: : @ @ @ @@@@@@ @@@@: ::: @@#
     |               @@:@@ @ ::@ @:: ::@ @@ @: :: : @ @ @ @@@@@@ @@@@: ::: @@#
     |            @@@@ :@@ @ ::@ @:: ::@ @@ @: :: : @ @ @ @@@@@@ @@@@: ::: @@#
     |       @@@::@@@@ :@@ @ ::@ @:: ::@ @@ @: :: : @ @ @ @@@@@@ @@@@: ::: @@#
     |    @@@@ @: @@@@ :@@ @ ::@ @:: ::@ @@ @: :: : @ @ @ @@@@@@ @@@@: ::: @@#
   0 +----------------------------------------------------------------------->Gi
     0                                                                   3.911
...
--------------------------------------------------------------------------------
  n        time(i)         total(B)   useful-heap(B) extra-heap(B)    stacks(B)
--------------------------------------------------------------------------------
 53  4,199,023,387    2,667,110,728    2,565,850,657   101,260,071            0
96.20% (2,565,850,657B) (heap allocation functions) malloc/new/new[], --alloc-fns, etc.
->92.58% (2,469,285,888B) 0x947720A: xmlHashCreate (in /usr/lib/x86_64-linux-gnu/libxml2.so.2.9.1)
| ->92.58% (2,469,285,888B) 0x94B006C: xmlXPathNewContext (in /usr/lib/x86_64-linux-gnu/libxml2.so.2.9.1)
|   ->92.58% (2,469,285,888B) 0x55DBB6D: _CFXMLNodesForXPath (in /home/djones6/.swiftenv/versions/4.0-DEVELOPMENT-SNAPSHOT-2017-06-12-a/usr/lib/swift/linux/libFoundation.so)
|     ->92.58% (2,469,285,888B) 0x596713F: Foundation.XMLNode.nodes(forXPath: Swift.String) throws -> [Foundation.XMLNode] (in /home/djones6/.swiftenv/versions/4.0-DEVELOPMENT-SNAPSHOT-2017-06-12-a/usr/lib/swift/linux/libFoundation.so)
|       ->92.58% (2,469,273,600B) 0x401EFD: main (in /home/djones6/tests/xmlLeak_sr)
@swift-ci
Copy link
Contributor Author

Comment by Matt Rajca (JIRA)

#1176

@swift-ci
Copy link
Contributor Author

Comment by Steven Van Impe (JIRA)

Thanks for fixing this. What release will this fix be available in?

@swift-ci
Copy link
Contributor Author

Comment by David Jones (JIRA)

The fix has been merged into the 4.0 branch: #1179

@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

1 participant