开发者

Xcode, Instruments: How to find an ever-increasingly leaking object?

开发者 https://www.devze.com 2023-02-12 07:06 出处:网络
Hard to explain what I need in the subject line. If you have a better idea, feel free to edit it. The problem is as follows:

Hard to explain what I need in the subject line. If you have a better idea, feel free to edit it.

The problem is as follows:

An iOS app handles documents, always one at a time. Every time it closes a doc and opens a new, even if it's the very same doc, memory usage increases constantly, and always by the same amount.

So there's a leak. The code doesn't free (release) something. Maybe it's an array that adds a reference to an item of the doc, and which never gets cleared, although it should.

Of course, the Leaks Instrument won't help here, since it's all "proper" (it only finds allocations without an owner).

How do I use Instruments (or dtrace) to find this bugger now? It would help if I could simply identify one of those ever-added objects in Instruments, the rest will be easy.

So far, I've been using the Allocations instrument, setting the inspection range to the time when the old doc gets closed and the new one gets opened. Still, this shows thousands of objects, and only one of them is the one that is left over from the previous doc and is still alive although it shouldn't. That one object I need to identify.

I imagine that what I need is a feature in the Allocations tool that subtracts all allocations just added and also subtracts all allocations that existed after startup - what would remain are items created by a new doc but not released since then. Is there some way to get the sets of collected objects, group them into sets and do set operatio开发者_Python百科ns like this? Probably not.

With about 20000 objects created every time the doc gets opened, and with about 50 source files involved, it's just not doable by looking at the source code nor by checking all the objects by hand.

Oh, and the static analyser doesn't help, either.


I think I figured it out:

The secret lies in the correct use of the Mark Heap button:

So far I only tried it once before and once after the loading of a new doc.

But when I to this three times (load, Heapshot, load, Heapshot, load, Heapshot), then the middle Heapshot will list the objects that are still alive from just between the two doc loads around it, which is exactly what I was looking for.


I would have thought the instruments tool would be ideal for this, just track allocations (not just leaks) and open/close a few documents to view these allocations coming through... Eventually click on the still allocated areas to see what's still being allocated and probably will allow you to work up the 'allocation tree' to find any root object(s) that still needs to be released.

I realize you probably know this already but for the benefit of newbies reading this, if you're doing this approach make sure NSZombies are disabled otherwise everything will still show up as being allocated I believe. Happy bug squashing...

0

精彩评论

暂无评论...
验证码 换一张
取 消