discuss-gnustep
[Top][All Lists]
Advanced

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

Re: Setter Gettor method style


From: Pascal Bourguignon
Subject: Re: Setter Gettor method style
Date: Sun, 4 Aug 2002 17:44:08 +0200 (CEST)

> From: Alexander Malmberg <alexander@malmberg.org>
> Date: Sun, 04 Aug 2002 17:07:34 +0200
> 
> > > > >   - (NSString *) title {
> > > > >        return [[title retain] autorelease];
> > > >
> > > > This is strictly equivalent to { return title; }
> > >
> > > This is not true. I've fixed bugs in Pantomime caused by this.
> > >
> > > - Alexander Malmberg
> > 
> > Agree.  But the case where the difference matter would not occur in my
> > book because I have the rule to retain all objects I need:
> > 
> >      NSString* title=[[stuff title]retain];
> >      [stuff setTitle:newTitle];
> >      NSLog(@"old title was %@\n",title);
> >      [title release];
> > 
> > In my  little mind, it's simplier  to have simple rules  to be applied
> > always without exceptions.
> 
> I agree, but I think the simple rule should be 'objects returned by
> library code are autoreleased', with the possible exception of pure
> container objects (eg. NSArray:s). Isn't one of the big points with
> having the whole autorelease system that you're supposed to be able to
> write 'sloppy' code and get away with it if you want to?
> 
> Anyway, whichever way it's supposed to be, it should be documented
> somewhere.

The rules I got along with the Foundation in NeXTSTEP 3.3 were:

    - initXxxx return retained objects, (POST: [result retainCount]==1)

    - classXxxx (stringWithFormat:, etc) return autoreleased objects,

    - accessors, since they return attributes don't return
      autoreleased objects, but retained objects 
      (POST: [result retainCount]>=1).


Note that if you use the following sequence:

      NSString* title=[stuff title];
      [stuff setColor:blue];
      NSLog(@"old title was %@\n",title);

There's  no reason to  retain the  title.  That  is, one  expects, and
should check, that the preconditions/postconditions of setColor: don't
have any side-effect on the title object returned before.

It's interesting  that some implementations of setTitle  may have this
side-effect  of deleting  an object  returned  by a  previous call  to
-title.

Note that for this reason, and the possibility to have mutable objects
as attributes or  passed as argument to settors, that  I may prefer to
take or to return copies of these objects.

In all  cases, these  behavior should be  documented, class  by class,
method by method, or a general convention should be used always.

But  as always,  my  main point  is  that this  should be  explicitely
documented.   The use  of return([[attribute  retain]autorelease]); is
not failproof (foolproof?!);

      NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];
      NSString* title=[stuff title];
      [stuff setTitle:@"New title"];
      [pool release];
      NSLog(@"old title was %@\n",title); // title is deleted...

or worse:

      NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];
      NSString* title=[stuff title];
      {
          NSAutoreleasePool* pool=[[NSAutoreleasePool alloc]init];
          NSString* title=[stuff title];
          [stuff setTitle:@"New title"];
          [pool release]; // title not deleted here.
          NSLog(@"old title was %@\n",title); // Ok here!
      }
      [pool release]; // title deleted here
      NSLog(@"old title was %@\n",title); // Not Ok here!


The  rule that  says that  you  must retain  the objects  you need  is
failproof in all cases.


-- 
__Pascal_Bourguignon__                   http://www.informatimago.com/
----------------------------------------------------------------------







reply via email to

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