• 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 management question (passing objects to method)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Memory management question (passing objects to method)


  • Subject: Re: Memory management question (passing objects to method)
  • From: Jiva DeVoe <email@hidden>
  • Date: Tue, 1 Mar 2005 01:24:50 -0700

Keep in mind, in both these cases, your NSStrings will be autoreleased because you are not explicitly allocing them or copying them. That said. The receiving methods may retain them if they expect to trigger events (ie: a run loop) to occur while they are running. But that shouldn't really matter to you. For your part, in both of these cases, no further memory management is required.

Now, lets say you did something like:

-(void)doSomething:(NSString *)aVar;
{
	localInstanceOfVar = aVar;
}

This code would be WRONG because you want aVar to be retained beyond the scope of your function (you are assigning it to a local instance variable).

You should instead do:

-(void)doSomething:(NSString *)aVar;
{
	localInstanceOfVar = [aVar retain];
}

and then ultimately:

-(void)dealloc;
{
	[localInstanceOfVar release];
	[super dealloc];
}

Memorize this:

Retention Count rules
1. Within a given block, the use of -copy, -alloc and -retain should equal the use of -release and -autorelease.
2. Objects created using convenience constructors (e.g. NSString's stringWithString) are considered autoreleased.
3. Implement a -dealloc method to release the instance variables you own


from: http://www.stepwise.com/Articles/Technical/2001-03-11.01.html

That's really all you need to know.

On Feb 28, 2005, at 11:09 PM, <email@hidden> wrote:

I have a question about memory management and passing objects created in one method to another method. When/where do I release these objects? Example, if you will...

- (void)doSomething
{
   NSString* test = @"test";
   [self doSomethingElse:test];
}

- (void)doSomethingElse:(NSString*)test
{
   NSLog(@"%@", test);
}

Now, these are my questions, and I apologize if they're rather basic, I tried searching for an answer and didn't really find one. When "test" is passed to doSomethingElse, should I retain it in doSomethingElse, and then release it before the method ends? Or should I release it in doSomething after it is passed to doSomethingElse, retain it in doSomethingElse and release it when doSomethingElse ends?

I'm curious about this because I don't know, and I want to make sure I practice good memory management. Thanks for any and all help. Feel free to link to me past posts/articles that have already answered this.

James

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


This email sent to email@hidden


--
Jiva DeVoe
http://www.devoesquared.com
PowerCard - Intuitive Project Management Software for Mac OS X

_______________________________________________
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: Memory management question (passing objects to method)
      • From: Evan Schoenberg <email@hidden>
  • Prev by Date: different CDs, same name = trouble?
  • Next by Date: Re: Memory management question (passing objects to method)
  • Previous by thread: Re: different CDs, same name = trouble?
  • Next by thread: Re: Memory management question (passing objects to method)
  • Index(es):
    • Date
    • Thread