Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-21-2011, 01:31 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 2
wjkoontz is on a distinguished road
Default How can an array contain a deallocated object?

I have a weird memory management problem.
I have created an NSMutableArray called "internalExpression" into which I store some objects (NSString's and NSNumber's).
I expose it as a read-only property called expression whose getter returns a NSArray copy:
@interface CalculatorBrain : NSObject {
@private
NSMutableArray *internalExpression;
}
@property (readonly) id expression;

Getter:
- (id)expression
{
NSArray *anExpression = [internalExpression copy];
[anExpression autorelease];
return anExpression;
}

Everything works fine, but after several times it crashes in the getter trying to copy the array.
I enabled NSZombie and found that the "internalExpression" array contains 4 objects, but the last one is a ZOMBIE!
How can an array contain a zombie?!
If I add an object to the array, the array should retain it.
How then do I end up with a 4 element array, with 3 good elements and 1 that has a release count of 0 ?
Here is what I see in Debug for internalExpression:
Early in run:
(gdb) po brain.internalExpression
<__NSArrayM 0x4b8fdb0>(
3,
+,
%x,
=
)

After crash in local window:
internalExpression __NSArrayM * 0x4b8fdb0
0 NSCFNumber * 0x4bbe010
1 NSCFString * 0x638ed00
2 NSCFString * 0x4bbe5a0
3 _NSZombie_CFString * 0x638f250

Somehow the string containing the "=" sign has been released.
Nowhere in my code do I remove objects from the array.
I add them, and whenever I'm done with the array I release the whole array.
However I have NOT done that between those two outputs (If I had the other 3 array elements would also have been gone).

Last edited by wjkoontz; 08-21-2011 at 01:36 AM.
wjkoontz is offline   Reply With Quote
Old 08-21-2011, 01:45 AM   #2 (permalink)
Just helping out.
 
Domele's Avatar
 
Join Date: Feb 2011
Posts: 2,565
Domele is on a distinguished road
Default

Post your code that you are using to insert objects into internal expression.
__________________
If you are looking for a quality developer, I'm your man. Give me a PM if you are interested.

New app - See screenshots and details at www.globaclock.com.

If you want to thank me, click the link. Every click counts. If you want to do more, buy my app. A link is available on my website. Thanks.
Domele is offline   Reply With Quote
Old 08-21-2011, 11:30 AM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Posts: 2
wjkoontz is on a distinguished road
Default I think I figured it out!

Quote:
Originally Posted by Domele View Post
Post your code that you are using to insert objects into internal expression.
For anyone experiencing similar problem, go back and comment out a bunch of calls to release (or all of them) and of course the problem will clear, and you will leak memory.
Now put them back one at at time, and it should help narrow down.
In my case there was two that would cause it.

I was releasing one of the strings I had put into the array.
I didn't need to because I didn't ever really own the string, it was a mutable copy, and I had put a pointer to it into a mutable array, then put a copy into an array, then released the second array, and released the string. OOPS, that's 2 releases for that string, so the original array lost one of its retain counts!

So it certainly is possible for an array to lose one of its members, if the member is also owned by anyone else who calls release too many times.
wjkoontz is offline   Reply With Quote
Reply

Bookmarks

Tags
array contains zombie, nsarray

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 395
13 members and 382 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, fiftysixty, gmarro, iOS.Lover, jonathandeknudt, raymng, stanny, tymex, UMAD, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:12 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0