• 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: Memory Mania Revisted
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory Mania Revisted


  • Subject: Re: Memory Mania Revisted
  • From: email@hidden
  • Date: Sun, 10 Feb 2002 09:45:45 -0800

The current documentation for NSObject indicates that +new is alive
and well.

In my guess, it is just a remnant of pre-OpenStep days.

Well, as for the theory, ie. whether it was "oficially" deprecated by some
whitepaper or not, let's somebody from Apple say -- I'm not sure.

In my experience, deprecated API is *always* marked as such in the doc and headers. I see no reason to think it has been deprecated.

What I *do*
know though is that (a) new and similar does not autorelease, (b) in many
places Apple explicitly says "but for copy, mutableCopy and alloc, anything
autoreleases /or returns a shared instance/" -- and (a) plus (b) to me
cleanly means "the +new family is deprecated and forgot".

A quick look in the doc found this in "Programming Topic: Memory Management":

The class methods alloc, allocWithZone: and new allocate memory for an object and initialize that object's reference to its class.

In "Programming Topic: The Root Class" it says, similarly,

The installation of the class structurethe initialization of isais one of the responsibilities of class methods that create (allocate memory for) new instances: alloc, allocWithZone: and new.

You are right that the doc also contains the statement (although I don't know about "many places" -- I found one place so far):

If you create an object (using alloc or allocWithZone:) or copy an object (using copy, copyWithZone:, mutableCopy, or mutableCopyWithZone:), you alone are responsible for releasing it.

This appears to me to simply be an omission. I've just logged 2858252 on it.

And as for the practice, would you kindly point out a few standard
Foundation or AppKit classes which use new (or some variant of newWith...) as
part of their standard API?

Of course most everybody inherits +new from NSObject, so many classes don't need to redefine it. I use +new on NSMutableArray, NSMutableDictionary and NSMutableString mostly. So the main "standard Foundation class" that I'd point to would be NSObject.
In gdb on Mail.app, "b new" gives:

[2] +[List new]
[3] +[NSCStringText new]
[4] +[NSObject new]
[5] +[NSSimpleHorizontalTypesetter new]
[6] +[NSView new]
[7] +[Object new]

By comparison, "b alloc" (and I think we agree alloc is not deprecated) gives:

[2] +[NSLeafProxy alloc]
[3] +[NSObject alloc]
[4] +[NSPageData alloc]
[5] +[NSPasteboard alloc]
[6] +[NSProxy alloc]
[7] +[Object alloc]

A breakpoint set on +[NSObject new] gets hit when a new compose window is made and when I paste, although it doesn't seem to get hit at any other time, I will admit. The others don't seem to get hit in Mail, at least as I'm using it right now.
However, a "b new" and then an autocomplete in gdb to find methods starting with "new" yields several dozen candidates, including things like "newSeparatorItem", "newRenderingRootStateWithMeasuring:" (which I'll admit I wrote while I was at Apple :->), "newAttributeDictionary", "newDictionaryFromDictionary:subsetMapping:zone:", "newInvocationWithMethodSignature:", etc. etc. I doubt that all of this is dead code. That it is generally private API is interesting, but use of "new" and related methods appears to be widespread at Apple nevertheless.

Also you might offer some ideas why -- if "new
is alive and well" -- those standard examples and recommendations use
[[NSAutoreleasePool alloc] init], or alike, although [NSAutoreleasePool new]
would work as well, and be even shorter?

My guess would be that Apple has decided that +new is confusing for novice programmers, and that an explicit alloc-init is clearer. And I would agree. Perhaps as a consequence of this decision, it does appear to have fallen into disuse in public API and examples. But that does not mean it's deprecated.
A quick grep through /Developer/Examples does turn up a few uses, although not nearly as many as uses of alloc:

NSAutoreleasePool *pool = [NSAutoreleasePool new];
./Foundation/MultiThreadedDO/MultiThreadedDO_main.m
result->lock = [NSLock new];
result->array = [NSMutableArray new];
./Foundation/MultiThreadedDO/ThreadSafeQueue.m
NSMutableString* theVersionStr = [[NSMutableString new] autorelease];
./Speech/SpeechSynthesisExamples/CocoaSpeechSynthesisExample/Sources/SpeakingTextWindow.
m

Sorry to beat a dead horse, but as newbies read this list and take what they read as gospel, I think it's important to stress that there appears to be no reason to believe that +new is deprecated.

Ben Haller
Stick Software
_______________________________________________
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: Memory Mania Revisted
      • From: Nicholas Shanks <email@hidden>
  • Prev by Date: Re: String fragment size?
  • Next by Date: Re: Memory Mania Revisted
  • Previous by thread: Re: Memory Mania Revisted
  • Next by thread: Re: Memory Mania Revisted
  • Index(es):
    • Date
    • Thread