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

retaining arrays


  • Subject: retaining arrays
  • From: Robert S Goldsmith <email@hidden>
  • Date: Fri, 21 Sep 2001 18:37:10 +0100

Hi :)

This is confusing me:

1)I create an NSMutableArray in main.m
2)I pass this to the init method of a class instance.
3)The class instance copies this pointer and stores it.

a quick mock up of the code:

in main.m
-----
NSMutableArray *temp;
testClass *classInstance;

temp=[NSMutableArray arrayWithCapacity:2];
[temp addObject:@"String 1"];
[temp addObject:@"String 2"];

classInstance=[[testClass alloc] initWithArray:temp];


Then in testClass
-----
-(id)initWithArray:(NSArray *)theArray
{
myPointerToArray=theArray; //myPointerToArray is
//an instance variable
//if testClass


Now I find that this 'eats' the array in main (it seems to send it a release message or something). To stop a sigsegv error later on (like if I tried to release the array temp) I have to send temp a retain message at some point (either in main before it is passed to the init call of the instance of testClass or in the init method before the pointer is copied to myPointerToArray).

Why?

Robert


  • Follow-Ups:
    • Re: retaining arrays
      • From: Drew McCormack <email@hidden>
    • Re: retaining arrays
      • From: Stéphane Sudre <email@hidden>
  • Prev by Date: Re: static typing (Learning Cocoa Chapter 13)
  • Next by Date: Re: NSSound--description forthcoming
  • Previous by thread: Re: static typing (Learning Cocoa Chapter 13)
  • Next by thread: Re: retaining arrays
  • Index(es):
    • Date
    • Thread