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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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, 12:24 PM   #1 (permalink)
Registered Member
 
mebarron's Avatar
 
Join Date: Apr 2009
Location: Michigan
Posts: 47
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, 01:09 PM   #2 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,091
Default

See the properties link in my signature for more information.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

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

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 02-09-2010, 01: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, 02:30 PM   #4 (permalink)
Registered Member
 
mebarron's Avatar
 
Join Date: Apr 2009
Location: Michigan
Posts: 47
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, 02:32 PM   #5 (permalink)
Emphasizing Fundamentals
 
BrianSlick's Avatar
 
Join Date: Jul 2009
Location: NoVA / DC Area
Age: 36
Posts: 7,091
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.
__________________
BriTer Ideas LLC - Code review, consulting, development. PM for pricing.

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

Are you a newbie? Things you should read:
BrianSlick is offline   Reply With Quote
Old 02-09-2010, 06:20 PM   #6 (permalink)
Registered Member
 
mebarron's Avatar
 
Join Date: Apr 2009
Location: Michigan
Posts: 47
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



» Advertisements
» Stats
Members: 158,480
Threads: 89,092
Posts: 380,125
Top Poster: BrianSlick (7,091)
Welcome to our newest member, premkumarmsc1
Powered by vBadvanced CMPS v3.1.0

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