Thread: Objective C Leaking

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 03:40 PM.

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, 01:47 AM
  2. C/C++ Objective questions
    By prayami in forum C++ Programming
    Replies: 22
    Last Post: 04-08-2008, 12: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, 08:12 AM
  5. gcc and objective c
    By Captain in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 10-13-2002, 03:03 PM