Advertise Books Events Forum News Social Networking Support Us

sdkIQ for iPhone
($4.99)

Shape Up
($0.99)

Your First iPhone App
($1.99)

iVidCam Free
(free)

Kid Art
($0.99)

iPUBQUIZ
(£1.19)

ArtStudio
($3.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 02-09-2010, 11:24 AM   #1 (permalink)
Registered Member
 
mebarron's Avatar
 
Join Date: Apr 2009
Location: Michigan
Posts: 31
Default Memory Management question about sample iPhone app code CoreDataBooks

I have a memory management question about sample iPhone app code from Apple, namely:
--------
CoreDataBooks:RootViewController.m

line 255 : // Release the adding managed object context.
line 256 : self.addingManagedObjectContext = nil;
--------

at line #256 the retainCount for addingManagedObjectContext == 1

shouldn't line #256 be the following:

[[self addingManagedObjectContext] release];

Otherwise there is a memory leak, Right?
mebarron is offline   Reply With Quote
Old 02-09-2010, 12:09 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: Loveland, OH
Age: 34
Posts: 1,972
Send a message via AIM to BrianSlick Send a message via Yahoo to BrianSlick
Default

See the properties link in my signature for more information.
__________________
My Company: BriTer Ideas LLC
My Services: Code review, consulting, development. PM for details.
My Apps: SlickShopper, Sports Minute (Or So)

Ways to thank me: Buy my app. Leave a PayPal donation.

Things I've Written That You Should Read:
Definitive Guide To Properties | iPhone Development Resources, Tips, and Tricks | UITableView Series (Last update: 2/24)
BrianSlick is offline   Reply With Quote
Old 02-09-2010, 12:36 PM   #3 (permalink)
dda
Registered Member
 
Join Date: Nov 2009
Posts: 42
Default

Quote:
Originally Posted by mebarron View Post
I have a memory management question about sample iPhone app code from Apple, namely:
--------
CoreDataBooks:RootViewController.m

line 255 : // Release the adding managed object context.
line 256 : self.addingManagedObjectContext = nil;
--------

at line #256 the retainCount for addingManagedObjectContext == 1

shouldn't line #256 be the following:

[[self addingManagedObjectContext] release];

Otherwise there is a memory leak, Right?
No. addingManagedObjectContext is an instance variable that is released in the dealloc method of the class it's part of.

Regards,

DDA
dda is offline   Reply With Quote
Old 02-09-2010, 01:30 PM   #4 (permalink)
Registered Member
 
mebarron's Avatar
 
Join Date: Apr 2009
Location: Michigan
Posts: 31
Default

Quote:
Originally Posted by dda View Post
No. addingManagedObjectContext is an instance variable that is released in the dealloc method of the class it's part of.

Regards,

DDA
Thanks so much for your reply.
Please help me out here. As I see it

line 255 : // Release the adding managed object context.
line 256 : self.addingManagedObjectContext = nil;

just prior to execution of line 255 and just after it too,
the retainCount == 1 on the object pointed to by addManagedObjectContext.

The command in the method dealloc would be:

[addingManagedObjectContext release];

This would result in the decrementation of the retainCount from 1 to zero
for the object pointed to by the variable:

addingManagedObjectContext .

Problem is, this variable/pointer has been set to nil
and the object that it used to point to is still sitting
in memory with a retainCount == 1 . Or ?

Furthermore, I don't see, any check for

if(addingManagedObjectContext == nil)

in the code, to use it as a flag. Please let me
know if I am missing something.
mebarron is offline   Reply With Quote
Old 02-09-2010, 01:32 PM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: Loveland, OH
Age: 34
Posts: 1,972
Send a message via AIM to BrianSlick Send a message via Yahoo to BrianSlick
Default

What you are missing is a good read of my properties thread. Go read it.

And you can't rely on retainCount, particularly if the object should be gone.
__________________
My Company: BriTer Ideas LLC
My Services: Code review, consulting, development. PM for details.
My Apps: SlickShopper, Sports Minute (Or So)

Ways to thank me: Buy my app. Leave a PayPal donation.

Things I've Written That You Should Read:
Definitive Guide To Properties | iPhone Development Resources, Tips, and Tricks | UITableView Series (Last update: 2/24)
BrianSlick is offline   Reply With Quote
Old 02-09-2010, 05:20 PM   #6 (permalink)
Registered Member
 
mebarron's Avatar
 
Join Date: Apr 2009
Location: Michigan
Posts: 31
Default

Quote:
Originally Posted by BrianSlick View Post
What you are missing is a good read of my properties thread. Go read it.

And you can't rely on retainCount, particularly if the object should be gone.
Thanks so much for taking the time. Thanks for the heads up about your
fine properties page. I had never before seen code for a setter such
as produces by @synthesize. I assumed that the retain/release protocol
had to be coded if you wanted it. All is clear now.

- (void)setMyArrayInstanceVariable: (NSArray *)anArray
{
[myArrayInstanceVariable release];
[anArray retain];
myArrayInstanceVariable = anArray;
}
mebarron is offline   Reply With Quote
Reply

Bookmarks

Tags
memory management, sample code

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


Enter the iPhone App Challenge!  Win $500!
» Advertisements
» Online Users: 722
15 members and 707 guests
ali.dar, Ed99, harry123456, headkaze, korki696, kristapsz, kuba1234, lokidil, maccoykung, Meoz, milanjansari, pereorra, SteveMobs, Thaurin, warcrow
Most users ever online was 779, 05-11-2009 at 09:55 AM.
» Stats
Members: 24,162
Threads: 38,934
Posts: 170,825
Top Poster: smasher (2,565)
Welcome to our newest member, ddustin
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 03:02 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.2.0