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

newbie memory question


  • Subject: newbie memory question
  • From: Daniel Child <email@hidden>
  • Date: Mon, 15 Nov 2004 16:46:17 -1000

Hi,

I wrote a simple app to count the letters in a textfield. In the code below, I checked for retain counts. A couple of people have suggested I use accessor methods to avoid memory leaks, but before I do so, I'd like to understand where things are getting retained so I can recognize such problems in the future. The console results were surprising to me.

At init, whatUserTyped retain count is: 2147483647 // ????? REALLY?????
At init, theLength retain count is: 2147483647

At countLetters, whatUserTyped retain count is: 4 // ALREADY?????
At countLetters, theLength retain count is: 1

At updateUI, whatUserTyped retain count is: 6
At updateUI, theLength retain count is: 3

At startOver, whatUserTyped retain count is: -1 // THE RESULT OF AUTORELEASE?
At startOver, theLength retain count is: 1

At updateUI, whatUserTyped retain count is: -1
At updateUI, theLength retain count is: 3


Does the very large number reflect anything? Or is it a memory address?
How is it that the second time whatTheUserTyped is called, it has a retain count of 4!!? And later of 6? And why does it get reset where theLength does not? Thanks.

(In the code, theStringToCount is a textfield where the user types the string, and theAnswer is another textfield where the count value is posted. All of this code is part of the MyDocument implementation file.)

- (id)init
{
self = [super init];
if (self)
{
whatUserTyped = [[NSString alloc] init];
theLength = [[NSString alloc] init];
numLetters = 0;
printf("At init, whatUserTyped retain count is: %i\n",
[whatUserTyped retainCount]);
printf("At init, theLength retain count is: %i\n\n",
[theLength retainCount]);
}
return self;
}

- (id)sender
{
whatUserTyped = [theStringToCount stringValue];
numLetters = [whatUserTyped length];
theLength = [NSString stringWithFormat: @"%i", numLetters];
printf("At countLetters, whatUserTyped retain count is: %i\n",
[whatUserTyped retainCount]);
printf("At countLetters, theLength retain count is: %i\n\n",
[theLength retainCount]);
[self updateUI];
}

- (id)sender
{
whatUserTyped = @"";
numLetters = 0;
theLength = [NSString stringWithFormat: @"%i", numLetters];
printf("At startOver, whatUserTyped retain count is: %i\n",
[whatUserTyped retainCount]);
printf("At startOver, theLength retain count is: %i\n\n",
[theLength retainCount]);
[self updateUI];
}

- (void)updateUI
{
[theStringToCount setStringValue: whatUserTyped];
[theAnswer setStringValue: theLength];
printf("At updateUI, whatUserTyped retain count is: %i\n",
[whatUserTyped retainCount]);
printf("At updateUI, theLength retain count is: %i\n\n",
[theLength retainCount]);
}

 _______________________________________________
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: newbie memory question
      • From: Scott Stevenson <email@hidden>
  • Prev by Date: Re: A framework for parsing HTML?
  • Next by Date: Re: Best way to catch signals in a Cocoa app
  • Previous by thread: Re: Custom NSToolbarItem
  • Next by thread: Re: newbie memory question
  • Index(es):
    • Date
    • Thread