• 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: A few questions about Memory Management
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: A few questions about Memory Management


  • Subject: Re: A few questions about Memory Management
  • From: Bob Ippolito <email@hidden>
  • Date: Fri, 11 Feb 2005 21:16:24 -0500


On Feb 11, 2005, at 8:56 PM, Adrian R. Foltyn wrote:

Hi there,

I have a couple of questions about Memory Management and hope that somebody finds time to help me out here.

----------------------------------------------------------------
If I dealloc a NSArray holding other objects that were previously alloced and inited the objects that were contained in the array are neither released nor dealloced?

Adding an object to an NSArray sends it a retain message. Removing an object from an NSArray sends it a release message. When an NSArray is deallocated, all of the objects it contains are removed from the NSArray first (and therefore, released).


----------------------------------------------------------------

If I understood correctly onelinecoding in Cocoa is bad if you care about how much memory your program uses. As an example:

Only for legibility, it is possible to write one-liners that have correct memory management semantics.


NSString *dreamWorld = [[[NSString initWithContentsOfFile:@"/path/to/realWorld.txt"] componentsSeparatedByString:@"hate"] componentsJoinedByString:@"love"];

With the above line I will never have the chance to send a release message to the NSString instance that stores the contents of the file and the NSArray instance that stores the delimited text as I don't have any pointers in place that reference to them. Neither the end of the method this code is executed in nor the deallocation of the object this method is used in will result in removing the NSString and NSArray instance from memory.

Not correct. Read up on NSAutoreleasePool. These objects are indeed released when the NSAutoreleasePool is popped, there is no memory leak.


So is there no other way of disposing of those objects than to say:

NSString *realWorld;
NSString *dreamWorld;
NSArray *delimitedRealWorld;

realWorld = [NSString initWithContentsOfFile:@"/path/to/realWorld.txt"];
delimitedRealWorld = [realWorld componentsSeparatedByString:@"hate"];
dreamWorld = [delimitedRealWorld componentsJoinedByString:@"love"];


[realWorld release];
[delimitedRealWord release];

This is actually not even correct! initWithContentsOfFile is an instance method and you're sending it to the class! You need to do [[NSString alloc] initWith....].


Anyway, the convention is that all methods except for +alloc, +new and -copy should return an object with a "net retain count" of zero. Meaning, unless you call one of these three methods, you do not have to send the result a release message.

If you corrected your example, a release message should be sent to realWorld, but NOT any of the other objects, because they have a net retain count of zero.

----------------------------------------------------------------
In the documentation for NSArray there is this example code:
NSArray *pathArray = [NSArray arrayWithObjects:@"System", @"Library", nil];
NSLog("The path is /%@.\n", [pathArray componentsJoinedByString:@"/"]);


Is it impossible to dealloc the resulting NSString instance of the componentsJoinedByString: call?
Would it be safe to say that example code in the documentation is not a good place to study memory management as it tries to demonstrate a concept in brief?

The example code is 100% correct, all objects in this example have a net retain count of zero. You *do not* need to worry about dealloc, ONLY about balancing retain and release. As mentioned before, there are special cases where the net retain count is one (+alloc, +new, -copy) so that these objects must be sent an additional release (meaning, exactly one more release than retain message).


----------------------------------------------------------------
If I would completely ignore memory management while writing a simple command line tool does the system free the objects in memory after the tool exits?


What happens to the memory if it crashes?

When a process terminates, all of the memory it was using is reclaimed.

-bob

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: A few questions about Memory Management
      • From: "John C. Warner" <email@hidden>
References: 
 >A few questions about Memory Management (From: "Adrian R. Foltyn" <email@hidden>)

  • Prev by Date: A few questions about Memory Management
  • Next by Date: Re: A few questions about Memory Management
  • Previous by thread: A few questions about Memory Management
  • Next by thread: Re: A few questions about Memory Management
  • Index(es):
    • Date
    • Thread