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 05-30-2009, 11:40 AM   #1 (permalink)
New Member
 
Join Date: May 2009
Posts: 2
rpierich is on a distinguished road
Exclamation Releasing an object is causing my app to crash.

In one of my controllers I have an array called videoGameItems (an instance variable) that contains dictionary objects. In my viewWillAppear method I call another method to load this list in from a sqlite database. In my load db method I

1. [videoGameItems release];

2. videoGameItems = [[NSMutableArray alloc] initWithCapacity: 100];

3. read in rows from the database and populate videoGameItems

The first time viewWillAppear is called everything works correctly. When I switch to a different view and then pop that view off the stack to return to my videogamelist view the application crashes on the [videoGameItems release]; line.

Does this sound like an error from memory management or something else?

Thank You,
Ruben
rpierich is offline   Reply With Quote
Old 05-30-2009, 06:16 PM   #2 (permalink)
New Member
 
Join Date: May 2009
Posts: 2
rpierich is on a distinguished road
Default

Also, I am using a UINavigationController to push the view onto the stack and then pop it off. Could that corrupt the videoGameItems variable (an instance variable)?


Quote:
Originally Posted by rpierich View Post
In one of my controllers I have an array called videoGameItems (an instance variable) that contains dictionary objects. In my viewWillAppear method I call another method to load this list in from a sqlite database. In my load db method I

1. [videoGameItems release];

2. videoGameItems = [[NSMutableArray alloc] initWithCapacity: 100];

3. read in rows from the database and populate videoGameItems

The first time viewWillAppear is called everything works correctly. When I switch to a different view and then pop that view off the stack to return to my videogamelist view the application crashes on the [videoGameItems release]; line.

Does this sound like an error from memory management or something else?

Thank You,
Ruben
rpierich is offline   Reply With Quote
Old 05-30-2009, 06:28 PM   #3 (permalink)
Registered Member
 
Join Date: May 2009
Location: Auckland, New Zealand
Posts: 16
Robby is on a distinguished road
Send a message via Skype™ to Robby
Default

Why are you calling Release before you alloc the object?
Release should only be called after alloc, and once you no longer need the memory.
Rob.
Robby is offline   Reply With Quote
Old 05-30-2009, 07:34 PM   #4 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by Robby View Post
Why are you calling Release before you alloc the object?
Release should only be called after alloc, and once you no longer need the memory.
Rob.
I think he's releasing the old list, and then creating a new one. That seems reasonable. On the first call videoGameItems is probably nil, and releasing nil has no effect.

If you get a crash when you release an object, it usually means that the object has been released too many times - you're releasing an object that has already been destroyed. Is there any other place where you're releasing videoGameItems?

Addendum: and no, pushing the view onto a stack should not change the instance variables.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 05-31-2009, 02:32 AM   #5 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

I also think that it sounds like the crash is due to over-releasing the videoGameItems member. If you are releasing this member somewhere else in your code, setting it to 'nil' after releasing it will probably get you fixed up.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Old 05-31-2009, 02:53 AM   #6 (permalink)
Pro. Game Developer
iPhone Dev SDK Supporter
 
Join Date: Feb 2009
Location: żLa Islas Hermosas?
Posts: 2,176
Kalimba is on a distinguished road
Default

Quote:
Originally Posted by Robby View Post
Why are you calling Release before you alloc the object?
Release should only be called after alloc, and once you no longer need the memory.
Rob.
It's easy to see how you might be confused by the posted code, since nearly all memory-management articles describe an "alloc then release" strategy, e.g. if you alloc/init it, you should later release it. I can't think of a single Obj-C/Cocoa memory management discussion that I've seen that doesn't take this same approach.

But in practice, the "release the old thing, then alloc the new thing" strategy is quite common. The basic thinking here is that the first time through the code, the "old thing" is nil, so releasing it does absolutely nothing, then the alloc/init (or the 'malloc', in plain C) makes the initial "new thing" assignment. Subsequent trips through the code will release the current member, then assign a new one.

One caveat of this "release then alloc" approach is that there really needs to be another release somewhere in the code to release the member when it's no longer needed, or the memory may be held onto longer than necessary and might possibly never be released. A problem arises when this alternate release doesn't put things into the proper state for the next trip through the "release then alloc" code. Specifically, if the member is released but is allowed to remain pointing at the released object, the app will crash the next time through the "release then alloc" code. This is because the member is again released, but it's not actually pointing to a valid object. Setting the member to nil after the alternate release properly fixes the member so the release then alloc code works properly without crashing.
__________________
~~ Word Flurry ~~ App Store / Website / Facebook
Kalimba is offline   Reply With Quote
Reply

Bookmarks

Tags
crash, memory management

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: 344
5 members and 339 guests
givensur, ipodphone, jbro, mer10, yomo710
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,113
Posts: 402,881
Top Poster: BrianSlick (7,990)
Welcome to our newest member, Anwerbl
Powered by vBadvanced CMPS v3.1.0

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