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 10-10-2009, 11:52 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 21
unplug is on a distinguished road
Default Crash when releasing NSDictionary

I created a NSDictionary through dictionaryWithContentsOfFile message in initWithStyle of my controller which is a subclass of UITableViewController. This NSDictionary is then assigned to an instance variable, mydictionary, and released.

In the dealloc, I have:
[self.mydictionary release]
and the program crash in this line. I have no idea why this is happening.

Can someone shed some lights here? Thanks.
Martin
unplug is offline   Reply With Quote
Old 10-11-2009, 02:44 AM   #2 (permalink)
iPhone Developer
 
kohjingyu's Avatar
 
Join Date: May 2009
Location: Singapore
Posts: 326
kohjingyu is on a distinguished road
Default

Did you release your dictionary somewhere else?

Usually adding

Code:
[self.mydictionary release];
self.mydictionary = nil;
should fix it, I think.
__________________
Bacteria Bash
Cheese Collect
Jokestar
Follow me on Twitter for news about my apps:
(Website|Twitter)
kohjingyu is offline   Reply With Quote
Old 10-11-2009, 08:13 AM   #3 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Don't send release messages to properties. If you want to clear out a property, do this:

Code:
[self setMyProperty:nil];
In dealloc, send the release message directly to the instance variable.

Code:
[myInstanceVariable release], myInstanceVariable = nil;
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Old 10-11-2009, 09:26 AM   #4 (permalink)
Registered Member
 
Join Date: Jan 2009
Location: http://www.developers-life.com
Posts: 35
sakrist is on a distinguished road
Default

set release only if your nsdictionary alloc or retain

?????????? ??????? ? Objective-c ??? iPhone - Notes of a Developer
Cocoa Touch: Memory management on the iphone
sakrist is offline   Reply With Quote
Old 10-11-2009, 08:37 PM   #5 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 21
unplug is on a distinguished road
Default

Quote:
Originally Posted by sakrist View Post
When I dig deeper, I found the following codes have problem.
Code:
	NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"mylist" ofType:@"plist"];
	NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
	self.locationList = [dict valueForKey:@"location"];
	[dict release];
This is in the viewDidLoad() of a UITableViewController, and this UITableViewController is created when a button is clicked.

The problem is it did not crash every time the button was clicked. The first time is always ok, but the 2nd or 3rd time when that button was clicked, it crashed.
unplug is offline   Reply With Quote
Old 10-11-2009, 08:39 PM   #6 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 21
unplug is on a distinguished road
Default

Code:
[myInstanceVariable release], myInstanceVariable = nil;
Do I have to use both, or just one? I think they are the same, right?
unplug is offline   Reply With Quote
Old 10-11-2009, 08:43 PM   #7 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,990
BrianSlick has a spectacular aura about
Default

Code:
	NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"mylist" ofType:@"plist"];
	NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:plistPath];
	self.locationList = [dict valueForKey:@"location"];
	[dict release];
You did not alloc, copy, new, or retain dict, so don't release it.

Quote:
Originally Posted by unplug View Post
Code:
[myInstanceVariable release], myInstanceVariable = nil;
Do I have to use both, or just one? I think they are the same, right?
You don't have to use both, and it probably doesn't matter in dealloc (use release). Elsewhere in code, it's a defensive coding measure. No, they are not the same thing.
__________________
BriTer Ideas LLC - Professional iOS App Development. Available for hire.

SlickShopper 2 | Free NSLog utility | Leave a PayPal donation.

Are you a newbie? Things you should read:
Definitive Guide To Properties | UITableView Series | Guide To Troubleshooting | Model Object Overview

Do you sit at a desk all day? Walk instead! Follow along with my treadmill desk adventures.
BrianSlick is offline   Reply With Quote
Reply

Bookmarks

Tags
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: 343
9 members and 334 guests
givensur, glenn_sayers, guusleijsten, ipodphone, mediaspree, mtl_tech_guy, Punkjumper, whitey99, yys
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,649
Threads: 94,114
Posts: 402,883
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:35 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0