【发布时间】:2010-10-20 03:38:21
【问题描述】:
我正在努力追踪我的 iPhone 程序中一些难以发现的内存泄漏。我正在对一个应用程序进行快速测试,该应用程序使用以下故意 - 错误 - 代码泄漏 NSString 对象:
-(void)applicationDidFinishLaunching:(NSNotification *)notification;
{
NSMutableString *test = [[NSMutableString alloc] init];
[test appendString:@"Testing 1"];
[test appendString:@"\nTesting 2"];
NSLog(@"%@", test);
// Uncomment the following line to release the
// string and clean up your leak.
// [test release], test = nil;
}
在应用程序的进程 ID 上运行泄漏后,我想出了以下内容:
sf$ leaks 3951
Process 3951: 9988 nodes malloced for 1260 KB
Process 3951: 3 leaks for 128 total leaked bytes.
Leak: 0x163b50 size=64 instance of 'NSCFDictionary', type ObjC, implemented in Foundation
0xa07e0720 0x01001080 0x0000000a 0x00000010 .~.............
0x0000000a 0x0000000c 0x0000000b 0x00000000 ................
0x00000000 0x00000015 0xa1b1c1d3 0x00163b90 .............;..
0x00163bd0 0x00000000 0x00000000 0x00000000 .;..............
Leak: 0x178190 size=32 string 'Testing 1
Testing 2'
Leak: 0x178210 size=32 instance of 'NSCFString', type ObjC, implemented in CoreFoundation
0xa02e24a0 0x010007ad 0x00178190 0x00000013 .$..............
0x00000020 0x00000200 0x00000000 0x00000000 ...............
现在,我们都知道漏洞在哪里了。这不是这个练习的重点,至少对我来说。我试图了解如何理解这个输出。有人告诉我有 3 处泄漏。
它们是位于内存地址 0x163b50、0x178190、0x178210 的对象。根据“泄漏”,他们的实现是在 Apple 框架中,而不是我的代码中。在下面这样一个简单的例子中,找到泄漏点并不难。但是,在一个有 500K 行代码的应用程序中,我发现泄漏的输出在这里毫无用处。
我做错了什么?如何理解此输出以帮助我找到我为清理内存泄漏而编写的代码中的罪魁祸首?
请注意,此线程不应提倡使用 Instruments 或 Clang 静态分析器。我已经清理了 Clang 静态分析器向我报告的所有内存泄漏。 Instruments 对我来说是臃肿且无信息的。我收到了大量关于泄漏的报告,但堆栈跟踪显示的任何一个都没有回到我自己的代码中——尽管我确信泄漏确实在我的代码中。我想在这里弄清楚如何使用命令行泄漏工具。
谢谢大家。
编辑:即使取消注释以清理泄漏,“泄漏”实用程序的抱怨甚至比泄漏时还要多。 Foundation/Cocoa 是否从这样一个微不足道的例子中泄漏了那么多?取消注释上述测试字符串的版本后会发生这种情况:
sf$ leaks 4383
Process 4383: 9890 nodes malloced for 1255 KB
Process 4383: 7 leaks for 560 total leaked bytes.
Leak: 0x163920 size=176 instance of 'NSPathStore2', type ObjC, implemented in Foundation
0xa07e2ae0 0x04f00000 0x0055002f 0x00650073 .*~...../.U.s.e.
0x00730072 0x0073002f 0x002f0066 0x0069004c r.s./.s.f./.L.i.
0x00720062 0x00720061 0x002f0079 0x00700041 b.r.a.r.y./.A.p.
0x006c0070 0x00630069 0x00740061 0x006f0069 p.l.i.c.a.t.i.o.
0x0020006e 0x00750053 0x00700070 0x0072006f n. .S.u.p.p.o.r.
0x002f0074 0x00490053 0x0042004d 0x002f004c t./.S.I.M.B.L./.
0x006c0050 0x00670075 0x006e0069 0x002f0073 P.l.u.g.i.n.s./.
0x00650054 0x006d0072 0x006e0069 0x006c0061 T.e.r.m.i.n.a.l.
...
Leak: 0x163350 size=160 instance of 'NSPathStore2', type ObjC, implemented in Foundation
0xa07e2ae0 0x04a00000 0x0055002f 0x00650073 .*~...../.U.s.e.
0x00730072 0x0073002f 0x002f0066 0x0069004c r.s./.s.f./.L.i.
0x00720062 0x00720061 0x002f0079 0x00700041 b.r.a.r.y./.A.p.
0x006c0070 0x00630069 0x00740061 0x006f0069 p.l.i.c.a.t.i.o.
0x0020006e 0x00750053 0x00700070 0x0072006f n. .S.u.p.p.o.r.
0x002f0074 0x00490053 0x0042004d 0x002f004c t./.S.I.M.B.L./.
0x006c0050 0x00670075 0x006e0069 0x002f0073 P.l.u.g.i.n.s./.
0x00650044 0x0069006c 0x00690063 0x0075006f D.e.l.i.c.i.o.u.
...
Leak: 0x1635a0 size=64 instance of 'NSCFDictionary', type ObjC, implemented in Foundation
0xa07e0720 0x01001080 0x0000000a 0x00000010 .~.............
0x0000000a 0x0000000c 0x0000000b 0x00000000 ................
0x00000000 0x00000015 0xa1b1c1d3 0x001635e0 .............5..
0x00163620 0x00000000 0x00000000 0x00000000 6..............
Leak: 0x163620 size=64
0xa02ed360 0x00160ee0 0x00163700 0xa02efc00 `........7......
0x00000000 0x00000000 0x00163680 0x00000000 .........6......
0x00000000 0x00000000 0x00163660 0xa02ed440 ........`6..@...
0xa02ec1a0 0xa02f0420 0x00000000 0x00163660 .... ./.....`6..
Leak: 0x163680 size=48 instance of 'NSCFString', type ObjC, implemented in CoreFoundation
0xa02e24a0 0x0100078c 0x6d6f6323 0x6c65642e .$......#com.del
0x6f696369 0x61737375 0x69726166 0x6c65442e icioussafari.Del
0x6f696369 0x61537375 0x69726166 0x00000000 iciousSafari....
Leak: 0x163660 size=32 instance of 'NSCFString', type ObjC, implemented in CoreFoundation
0xa02e24a0 0x0200078c 0x6c65440f 0x6f696369 .$.......Delicio
0x61537375 0x69726166 0x00000000 0x00000000 usSafari........
Leak: 0x160ee0 size=16 instance of 'NSCFString', type ObjC, implemented in CoreFoundation
0xa02e24a0 0x0100078c 0x362e3103 0x00000000 .$.......1.6....
【问题讨论】:
-
这是 Mac 应用还是 iphone 应用?你能在 iphone 上使用 mac 上的泄漏二进制文件吗?
标签: iphone objective-c cocoa cocoa-touch memory-leaks