• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: [Newbie] reference count
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Newbie] reference count


  • Subject: Re: [Newbie] reference count
  • From: Wesley Miaw <email@hidden>
  • Date: Sun, 18 May 2003 09:36:28 -0400

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

- - (id)init is called when the object is first instantiated. - (void)awakeFromNib is called once all of the NIB data has been loaded and connections have been established. This is done because some initialization can only take place after your NIB has been loaded, e.g. selecting the default GUI settings, populating menus, etc. - (id)init really is the initializer and - (void)awakeFromNib is a callback.

When you define - (id)init yourself for an object, you are overriding the - (id)init method provided by NSObject. Hence the call to [super init]. - (id)init is defined and called for every object unless you explicitly use some other init method (e.g. [[MyObject alloc] initWithMyOtherInitMethod]). If you do not define one yourself, it is still called but the definition is the one in NSObject. Note that any such - (id)initWithMyOtherInitMethod method should also have the call to [super init].

The correct thing to do here is to either move the code for or the call to - (void)_myInitInstanceVariables into - (id)init or - (void)awakeFromNib, but not to have it exist in both places.

Also, if for some reason you want it to exist in both places, then the reason for an extra NSMutableDictionary is clear: you've called [NSMutableDictionary alloc] twice but done no releasing, as is your responsibility to since you have called alloc. Your check for non nil is fine, but you can also do:

[_myOpenDocuments release];
_myOpenDocuments = [[NSMutableArray allocWithZone:[self zone]] init];

Calling release on a nil object is fine.

Don't forget that there should be a [_myOpenDocuments release] in - (void)dealloc.

On Sunday, May 18, 2003, at 09:04 AM, email@hidden wrote:

I think kunikyo is right. In the MYDocumentManager from "Cocoa
Programming", the following causes an extra mutable dictionary
allocation:

- (void)_myInitInstanceVariables
/*" Initialize instance variables: called by -init and -awakeFromNib.
"*/
{
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];

// Create array of open documents
_myOpenDocuments = [[NSMutableArray allocWithZone:[self zone]] init];

// Set initial cascade point
_myWindowCascadePoint = NSMakePoint(0, NSMaxY(screenVisibleFrame));
}


- (id)init
/*" Designated initializer "*/
{
self = [super init];

if(nil != self)
{
[self _myInitInstanceVariables];
}

return self;
}


- (void)awakeFromNib
/*" Called automatically after receiver is fully loaded from a nib
file. "*/
{
[self _myInitInstanceVariables];
}

It seems that both -init and -awakeFromNib are called at startup.

[snip]

Given this fact, the _myInitInstanceVariables method should have been
the following:
- (void)_myInitInstanceVariables
/*" Initialize instance variables: called by -init and -awakeFromNib.
"*/
{
NSRect screenVisibleFrame = [[NSScreen mainScreen] visibleFrame];

// Create array of open documents
if(nil == _myOpenDocuments)
{
_myOpenDocuments = [[NSMutableArray allocWithZone:[self zone]]
init];
}

// Set initial cascade point
_myWindowCascadePoint = NSMakePoint(0, NSMaxY(screenVisibleFrame));
}

There is other errata for MYDocumentManager at
http://www.cocoaprogramming.net/Errata.html

Now, the real question is why -init was called in the loading from nib
scenario ? Is it because there was no -initWithCoder: implementation ?
No. I just added conformance to the NSCoding protocol and -init was
still called.

I don't understand why -init was called in the loading from nib
scenario ?
- --
Wesley Miaw, Wesley Miaw Consulting
http://www.wesman.net/
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.7 (Darwin)

iD8DBQE+x4xfQv4agqRAk2kRAvvHAJ9BNuLNKBFruO4Y+qHGmldwloUsWQCgrqP/
U8WE6JcIbpFhkCm6Tz4p9pQ=
=kT1Q
-----END PGP SIGNATURE-----
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: [Newbie] reference count
      • From: publiclook <email@hidden>
  • Prev by Date: Re: Unit testing Cocoa-Java applications?
  • Next by Date: SOAP in ObjC?
  • Previous by thread: Re: [Newbie] reference count
  • Next by thread: Re: [Newbie] reference count
  • Index(es):
    • Date
    • Thread