• 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 memory question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: newbie memory question


  • Subject: RE: newbie memory question
  • From: "Jonathan E. Jackel" <email@hidden>
  • Date: Tue, 16 Nov 2004 12:43:48 -0500
  • Importance: Normal

> I removed the calls to alloc and init under the init method and simply ran
> the code as before. I still get an initially high retain count the first
> time I use the variables whatTheUserTyped and theLength. Do
> things hold onto
> to variables in the background?

Yes, sometimes.  In any event, you are only responsible for doing your part
by observing the retain/release rules.  Many newbies get confused by
querying the retain counts.  They see a high number and assume there is a
leak or that something else is wrong.  Just follow the retain release/rules
and you'll be fine.



>
> I have read the articles on memory you (and others) have suggested and was
> confused by one thing. If you have a setter method:
>
> (void) setVar: (NSFloat) f
> {
> [f retain]; // The assumption is f is autoreleased, but would autorelease
> dump it before I use //it here?
> [var release]; // This releases memory for what var previously pointed to?
> [var = f]; // and now we set it to f.
> }
>
> then doesn't f keep getting retained more than it should.

No

> (This was the
> format suggested in the article.) And if you release var, isn't there a
> danger that it's retain count drops to -1 (not retained)?

It's zero, not -1, and that's what you want.

Let's look at your code.  First off, there are a few notational mistakes,
and I assume you just made up NSFloat as an example, so let's just make it
an NSNumber:

> (void) setVar: (NSFloat) f

becomes

- (void):setVar:(NSNumber *)f

You need the -, which signifies that this is an instance method, and the *,
which signifies that f is really a pointer.

> {
> [f retain]; // The assumption is f is autoreleased, but would autorelease
> dump it before I use //it here?

No, autorelease works at the end of the event loop.

> [var release]; // This releases memory for what var previously pointed to?

Yes.  If var's retain count drops to zero, the old value disappears and the
memory is freed.  var may now be a pointer to garbage.  If some other part
of your program is using var, it should have retained it to make sure it
doesn't get garbage.

> [var = f]; // and now we set it to f.

Not right.  Should be

var = f;

No brackets.  var now points to what f points to, instead of garbage (or
where var used to be).

> In the code that I originally wrote, I tried releasing my variables after
> updateUI, but then when I called the countLetters again, the program
> crashed. If I did not want to use accessor methods, is there a way to
> release things safely?

This is why you use accessor methods -- to guarantee that things are
retained as long as they need to be, and released when they should be.

Now let's look at your revised code.

> - (IBAction)countLetters:(id)sender
> {
> whatUserTyped = [theStringToCount stringValue];
> numLetters = [whatUserTyped length];
> theLength = [NSString stringWithFormat: @"%i", numLetters];
> [self updateUI];
> }

Do you alloc/init, copy or retain any of these values?  No.  Then they are
not your responsibility to release.  In fact, releasing them will cause a
crash.

Go back and read those articles on memory management again.

Jonathan

 _______________________________________________
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

  • Prev by Date: Re: [Proposal] On-the-fly NIB localization
  • Next by Date: Re: A framework for parsing HTML?
  • Previous by thread: Re: newbie memory question
  • Next by thread: Question about NSImage and NSImageView
  • Index(es):
    • Date
    • Thread