views:

350

answers:

2

I'm having a severe memory leak issue with my program. I'm using Apple's Instruments to track my leaks, and in the first few seconds after my app starts there are hundreds and hundreds of leaks listed. The problem is none of them seem to tell me where the leak is coming from.

I've gone through all my classes and made sure that anything that was alloced was released at the end, and garbage collection is enabled as well. Another big problem is I tried starting up my app without garbage collection enabled and it just crashes.

Any advice?

Thanks

EDIT: If the source code is needed then I can email it

+2  A: 

Try running your project through AnalysisTool and see what it finds. It's essentially a GUI front-end for the Clang Static Analyzer. It will run through your code and find errors such as leaks and bad releases, among many other things. It will then present them to you in a step-by-step manner to help you better understand where you made mistakes.

It's a fantastic tool.

jbrennan
+1 I use the Clang Static Analyzer all the time
Dave DeLong
Many thanks guys, that tool is much better than Instruments. I'll see what I can fix :)
macatomy
I fixed everything reported by AnalysisTool and Instruments still reports a lot of memory leaks
macatomy
+2  A: 

Your question is tagged with "garbage collection".

Do you have GC turned on? If so, is it a command line tool? Did you call objc_startCollectorThread() as the first item in your main()?

If you have GC turned on, leaks analysis on Leopard will show quite a few false positives in certain circumstances. If you have access to Snow Leopard, I suggest you do the analysis there as the tools are significantly improved.

The clang static analyzer & Instruments are entirely orthogonal. You need to use both because the static analyzer isn't going to find all of the potential leaks in your code. In particular, it won't find situations where -- say -- you have unbounded cache growth or a global mutable set that is rooting your object graphs inadvertently.

Once you have fixed all of the problems the static analyzer finds, then use Instruments.

bbum
GC is turned on, its a Cocoa GUI app, not a comand line tool. I have fixed all the leaks reported by the analyzer but Instruments still reports a large number of memory leaks for reasons I don't know. I have no idea where the leaks in my app are occuring.
macatomy
I also have these malloc: free_garbage errors in the Xcode debugger console when I start my app:MyApp(9874,0xb0103000) malloc: free_garbage: garbage ptr = 0x101bfb0, has non-zero refcount = 1There are like 10 of those each time I start up, each with different address codes (e.g. 0x101bfb0)
macatomy
If you have access to it, use Snow Leopard to do your development. The memory analysis tools -- GC and non -- are a bazillion times better.
bbum