Search code examples
objective-ciosnsarrayautorelease

iOS Objec-C error for object pointer being freed was not allocated


I'm getting the following error in xcode.

error for object 0x4e18d00: pointer being freed was not allocated ** set a breakpoint in malloc_error_break to debug

I've setup NSZombieEnabled in the target so I can view the call. It is

-[___NSArrayI release]

It looks like i've released some array somewhere else in my code and then the auto release pool is trying to release it also when it is already dealloc'd.

How can I find out where? Any idea?

FYI, I'm creating all my arrays using the arrayWithCapacity method or something similar, never use alloc or init methods. I don't see, anywhere where I am releasing those same arrays. (maybe i'm blind!!)

Also, the control flow is as follows: I click an UIButton, fire the function attached to onclick. This will go to various logic layers and then get an NSArray back. I can then iterate this array in the "onClick button function" and print the contents. When this "onClick button function" quits I get the above error in the "main" method.

Another note is that in one function I create an NSMutableArray but want to return an NSArray so I use [[mutableArray copy] autorelease]. This ok, right?

Any advice would be greatly appreciated as I often have great difficulty in trying to track the cause for errors down.

Thanks in advance.


Solution

  • I figured out what was wrong..

    In book class I had chapters declared as an NSAray and in the default constructor I said

    chapters = [NSArray array];
    

    I took that out of the constructor and all is well. Thanks for your help guys.

    P.S. If I forget can somebody please mark this as the accepted answer? Cheers ;)