Objective C Leaking

This is a discussion on Objective C Leaking within the Tech Board forums, part of the Community Boards category; Hello all, I have only just started coding objective C, and I tried to code something simple: a class that ...

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262

    Objective C Leaking

    Hello all,

    I have only just started coding objective C, and I tried to code something simple: a class that stores a string and then retrieves it.
    Code:
    #import <Cocoa/Cocoa.h>
    #import <stdio.h>
    
    @interface Person: NSObject {
      NSString *name;
    }
    
    @property (retain) NSString* name;
    
    -(void) dealloc;
    
    @end
    
    
    @implementation Person
    
    @synthesize name;
    
    -(id) init
    {
      if(self = [super init])
      {
        [self setName:nil];
      }
      return self;
    }
    
    -(void) dealloc
    {
      printf("Calling dealloc\n");
      self.name = nil;
      [super dealloc];
    }
    
    @end
    
    
    
    int main(int argc, char *argv[])
    {
      Person *person = [[Person alloc] init];
      NSString *str = [[NSString alloc] initWithCString:"Test"];
      
      person.name = str;
      NSLog(@"%@", person.name);
      
      [str release];
      [person release];
    }
    However, when running it it reports "autoreleased with no pool in place". Why? It seems to be in the synthesized name setter. From the tutorial I read, this should be fine - do I always need an autorelease pool if I use "@synthesize"? Where should I make it - it's probably not good to have it always in main, nor is it very useful to set up and releasing a pool every time I use a setter? [edit: I expected that the autoreleased objects would only be released after the autorelease pool is released, but maybe this is not true?]

    Also, what does Objective C say about the default values of the members? Right now I set it to nil manually; do I need to, or is that standard? Also, do I need [super init] here, and do I need it if I don't need an "init" function, or does the default init function call [super init]?

    Okay, maybe I should get myself an Objective C book, as the tutorials were dreadful (actually, one seemed quite good, but way too short)...

    Thanks in advance
    Last edited by EVOEx; 09-07-2010 at 04:40 PM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    I think that what you have to do is release name explicitly in the destructor:
    Code:
    -(void) dealloc
    {
      printf("Calling dealloc\n");
      [name release]
      [super dealloc];  
    }
    EDIT: Was this one of the tutorials you looked at? It looks fairly decent for someone with previous programming experience.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by bernt View Post
    I think that what you have to do is release name explicitly in the destructor:
    Code:
    -(void) dealloc
    {
      printf("Calling dealloc\n");
      [name release]
      [super dealloc];  
    }
    EDIT: Was this one of the tutorials you looked at? It looks fairly decent for someone with previous programming experience.
    Thanks. And yes, that was one of the tutorial I looked at and it was quite decent although lacking loads of stuff (I still have no idea what NSNumber really is in regard to integers or what you can do with an NSString and how).

    Anyways, I tried what you said, but it didn't matter - still the same error. I don't think it should anyway, the setter would retain nil (doing nothing) and autorelease the old name. That's what the tutorial told me anyway (the one you linked me to).

    But I did some testing and I found out that the problem is the getter of the name: apparently it adds one to the retain count. Does that mean I have to release everything I get from the getter? Quite dreadful if you want to do simple things like printing it in this case. I mean - if I'd want to actually retain it I'd call it myself in that case, right?

    So what's going on here? Why's the getting incrementing the retain count? (I actually printed it to verify - it was 2 at the dealloc function.

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Okay, let me answer my own questions for people who find this post while searching. I've been working through a book (it's horrible reading through stuff most of which you already know though)...

    Here are the answers:

    Quote Originally Posted by EVOEx View Post
    However, when running it it reports "autoreleased with no pool in place". Why? It seems to be in the synthesized name setter. From the tutorial I read, this should be fine - do I always need an autorelease pool if I use "@synthesize"? Where should I make it - it's probably not good to have it always in main, nor is it very useful to set up and releasing a pool every time I use a setter? [edit: I expected that the autoreleased objects would only be released after the autorelease pool is released, but maybe this is not true?]
    Yes, you do always need the autorelease pool when using autorelease because it doesn't directly decrement the reference count - only when draining the pool. Luckily pools can be nested...

    Quote Originally Posted by EVOEx View Post
    Also, what does Objective C say about the default values of the members? Right now I set it to nil manually; do I need to, or is that standard? Also, do I need [super init] here, and do I need it if I don't need an "init" function, or does the default init function call [super init]?
    From what I've read, yes, all the members are set to nil by default. Good, good. Also, about whether I need an init function: stupid question, it's the basic rules of inheritence.

    Quote Originally Posted by EVOEx View Post
    Okay, maybe I should get myself an Objective C book, as the tutorials were dreadful (actually, one seemed quite good, but way too short)...
    "Programming in Objective C" is quite good, even though it's very longwinded for C programmers. I read it in a couple of hours...


    Thanks bernt for your help ;-).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. STL leaking?
    By g4j31a5 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2009, 02:47 AM
  2. C/C++ Objective questions
    By prayami in forum C++ Programming
    Replies: 22
    Last Post: 04-08-2008, 01:58 PM
  3. Objective C
    By Shadow12345 in forum C++ Programming
    Replies: 19
    Last Post: 01-15-2003, 01:40 PM
  4. Programming Objective C in Mac OSX
    By MacUser in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 10-17-2002, 09:12 AM
  5. gcc and objective c
    By Captain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-13-2002, 04:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21