• 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: Autorelease pool questions
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Autorelease pool questions


  • Subject: Re: Autorelease pool questions
  • From: Bill Bumgarner <email@hidden>
  • Date: Mon, 20 Jan 2003 16:11:46 -0500

On Sunday, Jan 19, 2003, at 21:08 Europe/Stockholm, Rakesh Pandey wrote:
In my code I create two NSString object like this:

NSString *myString = [[NSSTring alloc] init];
NSString *myString1 = [[NSString initWithString:@"Rakesh"];

On Monday, Jan 20, 2003, at 13:05 US/Eastern, Joar wrote:
Now back to your printed logs. I'm surprised that myString gives you
trouble, I would have expected it to be the other way around since the
strange large number printed for that variable looks like it's the
result of printing a non-initialized value - like I would have expected
the myString1 to be.

Assuming that Joar's corrections to your code are correct, the output is as expected.

NSString is a class cluster.

That is, simply allocating a string-- [NSString alloc]-- yields an instance that has not be intialized. Calling one of the various random -init methods will typically return a *different* instance than the one that was originally allocated. That is, the NSString class cluster does not have enough information to efficiently instantiate the immutable string instance until it knows what that immutable instance will contain.

So, [[NSString alloc] init] is nonsensical or, at the least, represents a zero length string. A -retainCount of 2147483647 smells like -1 cast to unsigned int or else it is just a really big number.

In any case, it is very likely that the NSString class cluster optimizes the -init implementation on the NSString class to return a singleton... hell, given that it is three lines of code to test:

....
fprintf(stdout, "[[NString alloc] init] = 0x%x\n", [[NSString alloc] init]);
fprintf(stdout, "[[NString alloc] init] = 0x%x\n", [[NSString alloc] init]);
fprintf(stdout, "[[NString alloc] initWithString: @\"foo\"] = 0x%x\n",
[[NSString alloc] initWithString: @"foo"]);
fprintf(stdout, "[[NString alloc] initWithString: @\"foo\"] = 0x%x\n",
[[NSString alloc] initWithString: @"foo"]);
....

Output:

[[NString alloc] init] = 0xa0923420
[[NString alloc] init] = 0xa0923420
[[NString alloc] initWithString: @"foo"] = 0x555e0
[[NString alloc] initWithString: @"foo"] = 0x55840

So, yes, that is exactly what is happening.

And there you have it....

b.bum
No Chunks...
... No Foul!
_______________________________________________
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.

  • Prev by Date: Re: Making opaque images transparent
  • Next by Date: Re: dragable divider
  • Previous by thread: Re: How to kill a thread? (Thanks)
  • Next by thread: Re: Autorelease pool questions
  • Index(es):
    • Date
    • Thread