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-04-2011, 12:30 PM   #1 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 10
keiffer6 is on a distinguished road
Question Help resolving memory leak

I have a class named Ephemerides that does large amount of calculations and then returns an array of the results. I call this class from one of my view controllers in viewWillAppear. Everything works fine, but when I use instruments to check for leaks this always shows up as a leak. I have tried everything I can think of to release the object but so far I have had no luck.

Here is the relevant viewWillAppear code that calls the class:
Code:
    [self setEphemText:[[Ephemerides alloc] initWithName:[infoText objectAtIndex:16] 
                                                      qq:[[infoText objectAtIndex:6] doubleValue] 
                                                       e:[[infoText objectAtIndex:7] doubleValue] 
                                                       w:[[infoText objectAtIndex:8] doubleValue] 
                                                      om:[[infoText objectAtIndex:9] doubleValue] 
                                                       I:[[infoText objectAtIndex:10] doubleValue] 
                                                 yr_peri:[[infoText objectAtIndex:3] doubleValue] 
                                                 mo_peri:[[infoText objectAtIndex:4] doubleValue] 
                                                day_peri:[[infoText objectAtIndex:5] doubleValue] 
                                                 abs_mag:[[infoText objectAtIndex:14] doubleValue] 
                                                   mag_n:[[infoText objectAtIndex:15] doubleValue] 
                                                   coord:coordRA 
                                                   steps:numberSteps 
                                                interval:numberInterval]];

    [self setTableDataSource:ephemText];
     
    [self.infoTable reloadData];
infoText is an array that is loaded from a SQLite table prior to this and contains the necessary variables for the Ephemerides class to do the calculations.

Whenever I load that view controller in the app, I get this from Instruments:
Code:
#	Address	Category	Event Type	RefCt	Timestamp	Size	Responsible Library	Responsible Caller
0	0xa9399e0	Ephemerides	Malloc	1	00:08.502.430	16	Comet	-[CometEphem getEphemerides]
I do have [Ephemerides release]; and [ephemText release]; in my dealloc. That does not seem to help any. If I comment out the section of code shown above, then the memory leak goes away. So I am pretty certain that the problem is with that section of code.

I am relatively new to Xcode, but I understand the concepts of releasing memory. When I run my app in the simulator this is the only leak I have and I just can't figure out how to resolve it. Each time that controller loads I get another 16 bytes. My guess is that I should be going a about calling the Ephemerides class differently. Any help or direction would be greatly appreciated.

Keith
keiffer6 is offline   Reply With Quote
Old 08-04-2011, 12:40 PM   #2 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

I'd guess that your dealloc method is never getting called. Put a breakpoint on it and see if it ever hits.
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Old 08-04-2011, 12:58 PM   #3 (permalink)
Super Moderator
 
Join Date: Oct 2009
Location: San Diego, CA
Posts: 1,586
JasonR is on a distinguished road
Default

When you call setEphemText: with an object you created with alloc on the same line, the object is getting retained twice, and only released once. Read BrianSlick's link about properties to understand why this is wrong, and how to fix it. You'll see his name to the right of this post, listed as Top Poster. Then you'll see his property link in his signature.
__________________
My development blog: http://jrinn.com
JasonR is offline   Reply With Quote
Old 08-04-2011, 02:36 PM   #4 (permalink)
Registered Member
 
Join Date: Jun 2011
Location: New York
Posts: 18
TheStuFactor is on a distinguished road
Default

Your Ephemerides instance is never being released.

Set your Ephemerides as an object before you call '[self setEphemText]' and then release that object after your method call.

Code:
  // instantiate an instance of Ephemrides
  Ephemerides *eph = [[Ephemerides alloc] initWithName:....];
  
  // call your method
  [self setEphemText: eph];

  // release the instance
  [eph release];

  .
  .
  .
TheStuFactor is offline   Reply With Quote
Old 08-04-2011, 02:41 PM   #5 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 10
keiffer6 is on a distinguished road
Default

Quote:
Originally Posted by JasonR View Post
When you call setEphemText: with an object you created with alloc on the same line, the object is getting retained twice, and only released once. Read BrianSlick's link about properties to understand why this is wrong, and how to fix it. You'll see his name to the right of this post, listed as Top Poster. Then you'll see his property link in his signature.
Thanks for pointing me to BrianSlick's link. That is really good information. I have read through it and made the following changes, but I still get the same leak.

Code:
    NSArray *tempEphem = [[Ephemerides alloc] initWithName:[infoText objectAtIndex:16] 
                                                            qq:[[infoText objectAtIndex:6] doubleValue] 
                                                             e:[[infoText objectAtIndex:7] doubleValue] 
                                                             w:[[infoText objectAtIndex:8] doubleValue] 
                                                            om:[[infoText objectAtIndex:9] doubleValue] 
                                                             I:[[infoText objectAtIndex:10] doubleValue] 
                                                       yr_peri:[[infoText objectAtIndex:3] doubleValue] 
                                                       mo_peri:[[infoText objectAtIndex:4] doubleValue] 
                                                      day_peri:[[infoText objectAtIndex:5] doubleValue] 
                                                       abs_mag:[[infoText objectAtIndex:14] doubleValue] 
                                                         mag_n:[[infoText objectAtIndex:15] doubleValue] 
                                                         coord:coordRA 
                                                         steps:numberSteps 
                                                      interval:numberInterval];
    [self setEphemText: tempEphem];
    [tempEphem release], tempEphem = nil;
Code:
#	Address	Category	Event Type	RefCt	Timestamp	Size	Responsible Library	Responsible Caller
0	0x7c464b0	Ephemerides	Malloc	1	00:16.883.589	16	Comet	-[CometEphem getEphemerides]

Am I just missing something?
keiffer6 is offline   Reply With Quote
Old 08-04-2011, 02:42 PM   #6 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 10
keiffer6 is on a distinguished road
Default

Quote:
Originally Posted by TheStuFactor View Post
Your Ephemerides instance is never being released.

Set your Ephemerides as an object before you call '[self setEphemText]' and then release that object after your method call.

Code:
  // instantiate an instance of Ephemrides
  Ephemerides *eph = [[Ephemerides alloc] initWithName:....];
  
  // call your method
  [self setEphemText: eph];

  // release the instance
  [eph release];

  .
  .
  .
Stu,

That is basically what I have at this point but I still get the leak.

The big difference is that I have NSArray *tempEphem where as you say to do Ephemerides *tempEphem. I have tried this but I always get an "Incompatible pointer types initializing 'Ephemerides *' with an expression of type 'NSArray *'"



Keith

Last edited by keiffer6; 08-04-2011 at 02:45 PM. Reason: more info
keiffer6 is offline   Reply With Quote
Old 08-04-2011, 03:40 PM   #7 (permalink)
Registered Member
 
Join Date: Mar 2009
Posts: 10
keiffer6 is on a distinguished road
Default

Sorry, sometimes I can just be a little slow

I modified my code to this:
Code:
    Ephemerides *eph = [[Ephemerides alloc] init];
    NSArray *tempEphem = [eph initWithName:[infoText objectAtIndex:16] 
                   qq:[[infoText objectAtIndex:6] doubleValue] 
                    e:[[infoText objectAtIndex:7] doubleValue] 
                    w:[[infoText objectAtIndex:8] doubleValue] 
                   om:[[infoText objectAtIndex:9] doubleValue] 
                    I:[[infoText objectAtIndex:10] doubleValue] 
              yr_peri:[[infoText objectAtIndex:3] doubleValue] 
              mo_peri:[[infoText objectAtIndex:4] doubleValue] 
             day_peri:[[infoText objectAtIndex:5] doubleValue] 
              abs_mag:[[infoText objectAtIndex:14] doubleValue] 
                mag_n:[[infoText objectAtIndex:15] doubleValue] 
                coord:coordRA 
                steps:numberSteps 
             interval:numberInterval];
                              
    [self setEphemText: tempEphem];
    [eph release], eph = nil;
    [tempEphem release], tempEphem = nil;
That appears to have solved my leak problem. Thank you very much for all the help and direction.
keiffer6 is offline   Reply With Quote
Reply

Bookmarks

Tags
alloc, class, release, xcode

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: 394
14 members and 380 guests
7twenty7, blasterbr, buggen, chiataytuday, dedeys78, dre, fiftysixty, HemiMG, jimmyon122, jonathandeknudt, LEARN2MAKE, pungs, tymex, UMAD
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:03 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0