嗨,我是新手。我正在编写一个函数来将一些数据写入xml文档。
我在网上遇到内存泄漏。
xmlDoc.XMLDataWithOptions(Int(NSXMLNodePrettyPrint) | Int(NSXMLDocumentIncludeContentTypeDeclaration))
代码段如下。我认为xml对象存在写入文件的问题。当在仪器工具中看到指出上述线时。因为程序中的这种记忆在一段时间后不断增长。 :
func WriteToFile(FileName: String,
bIsOffline: BooleanType,
StartTime: String,
EndTime: String,
ToolName: String,
Url: String,
WinTitle: String,
WindowID: Int,
ExeName: String,
CmdLine: String,
PID: Int)
{
let HostName = NSHost.currentHost().name as String!
let HostNameBase64 = ConvertStrToBase64(HostName.lowercaseString)
// if (NSFileManager.defaultManager().fileExistsAtPath(FileName)) {
var StatObj = NSFileManager.defaultManager().attributesOfItemAtPath(FileName, error: nil) as Dictionary!
if (StatObj != nil) {
let FileSize = StatObj["NSFileSize"] as NSNumber
//var FileSize = FileSizeStr.toInt()
var xmlDoc: NSXMLDocument!
var RootNode: NSXMLElement
if (FileSize.unsignedLongValue > 0) {
let InputStr = NSURL.fileURLWithPath(FileName, isDirectory: false)
xmlDoc = NSXMLDocument(contentsOfURL: InputStr!, options: Int(NSXMLDocumentTidyXML), error: nil)!
RootNode = xmlDoc.rootElement() as NSXMLElement!
} else {
RootNode = NSXMLElement(name: "test")
xmlDoc = NSXMLDocument(rootElement: RootNode)
// let Root = xmlDoc.rootElement()
xmlDoc.characterEncoding = "UTF-8"
xmlDoc.standalone = true
}
let WinInfoNode = NSXMLElement(name: "TEMP1")
let TmpNode = NSXMLElement(name: "TEMP2", stringValue: StartTime)
let TmpNode1 = NSXMLElement(name: "TEMP3", stringValue: EndTime)
let TmpNode2 = NSXMLElement(name: "TEMP4", stringValue: HostNameBase64)
WinInfoNode.addChild(TmpNode)
WinInfoNode.addChild(TmpNode1)
WinInfoNode.addChild(TmpNode2)
RootNode.addChild(WinInfoNode)
var DataToWrite: NSData = xmlDoc.XMLDataWithOptions(Int(NSXMLNodePrettyPrint) | Int(NSXMLDocumentIncludeContentTypeDeclaration))
DataToWrite.writeToFile(FileName, atomically: true)
}
// }
}
任何帮助指针都将受到赞赏。