• 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] mem errors cont.
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Newbie] mem errors cont.


  • Subject: Re: [Newbie] mem errors cont.
  • From: Danny Swarzman <email@hidden>
  • Date: Sun, 3 Aug 2003 02:22:25 -0700

In order for this to work, lineArray must have been assigned a value before
this was called. The value needs to have memory allocated.

lineArray = [[NSArray alloc]init];

or

lineArray = [NSArray arrayWithObjects:...

If you did it the first way, you allocated the memory and eventually you
will need to release it with

[lineArray release];

But if you did it the second way, the object is put into the auto-release
pool and will be deallocated before your button action is called. To
prevent that, right after assigning to lineArray do

[lineArray retain];

In this case, you still need to do:

[lineArray release];

when you know you won't need the data any more.

There are many ways to get into trouble with memory in Cocoa. Take a look
at the archives of this list. You are not alone.

This is discussed in the Objective-C reference from the Apple site.
Unfortunately the style is more folksy than precise on this subject.

The rules are:

When an object is created with alloc or copy, it has a retain count of 1.
When an object is created with arrayWithObjects or any similar, it has a
retain count of 1 and is put into the auto-release pool.
A call to retain increments the retain count by one.
A call to release decrements the retain count by one.
When the retain count of an object goes from 1 to 0, the object disappears.
On each cycle of the auto-release pool, each object is removed from the
pool and its release method is called.
The objects in the default auto-release pool are released in the main event
loop.

Conventions followed by container classes:

When an object is put into a dictionary or array with a call such as
NSMutableArray's addObject:, the retain count in incremented.
When an object is removed from a dicitonary or array with a method such as
NSMutableArray's removeObject:, the retain count is decremented.


At 23:53 -0700 8/2/03, Michael Hanna wrote:
>When the user clicks an IB button, this method gets called. Seems
>whenever I reference an NSArray called lineArray I get a signal 10
>error.
>
>For instance here:
>
>NSLog(@"lineArray count: %u", [lineArray count]);
>
>and here:
>
> for(i = 0 ; i < [lineArray count] ; i++)
> {
> [....]
> }
>
>this is valid cocoa code right? This is driving me a little cuckoo..
>Michael
>
>
>////////
>//CODE//
>
>- (IBAction)generate:(id)sender
>{
>
> int i;
> int phraseNum = -1;
> //int lineCount;
> //int randIndex; // index of random phrase
> NSString *tokenString = @"%s";
> NSString *theLine;
>
> NSLog(@"got here 1"); // will print this
>
> NSLog(@"lineArray count: %u", [lineArray count]); // signal 10
>here!(?)
>
> NSLog(@"got here 2"); // won't print this
>
> // a for-loop for each line in lineArray
> for(i = 0 ; i < [lineArray count] ; i++) // signal
>10 on this
>line too
> {
> // copy over the line at index to the line NSString
>
> NSLog(@"entered for");
> // never gets here
>
> /*... continues... */
>
> ... }
>
>//CODE//
>////////
>_______________________________________________
>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.
_______________________________________________
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.

References: 
 >[Newbie] mem errors cont. (From: Michael Hanna <email@hidden>)

  • Prev by Date: Re: beginning C part 2
  • Next by Date: Re: Corrupted NSMutableString
  • Previous by thread: [Newbie] mem errors cont.
  • Next by thread: Re: [Newbie] mem errors cont.
  • Index(es):
    • Date
    • Thread