discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Introduce me to NSTableView


From: Chris B. Vetter
Subject: Re: Introduce me to NSTableView
Date: Fri, 6 Jun 2003 09:33:22 -0700

On Fri, 6 Jun 2003 17:42:16 +0300
Christopher Culver <crculver@users.sourceforge.net> wrote:
> As a general rule of thumb, should I call [... release] for every GUI 
> element after I add it to a container?

That depends on how you want to further use this object.

As a general rule, if you alloc/init an object, you should release it
within the same scope. That is, if you alloc/init an object in a method
and it is only used inside that method, you should release it before
leaving the method:

  - (void) aSampleMethod
  {
    NSString *string = [[NSString alloc] init];

    // do something with string

    [string release]; // balance alloc/init
  
    return;
  }

If you have an instance variable, you usually initialize it in -init and
release it in your class' -dealloc:

  - (id) init
  {
    if( (self = [super init]) )
    {
      someVariable = [[NSMutableArray alloc] initWithCapacity: 10];

      // ...

      return self;
    }

    return nil;
  }

  - (void) dealloc
  {
    someVariable = nil;
    [someVariable release]; // balance alloc/init in -init

    [super dealloc];

    return;
  }

There's a couple of tutorials on memory management in Objective-C at

http://wiki.gnustep.us/ -> Tutorials -> Objective-C -> Memory Management

that explain how to use alloc, init, retain, autorelease and release.

-- 
Chris




reply via email to

[Prev in Thread] Current Thread [Next in Thread]