Memory Management
Memory Management
- Subject: Memory Management
- From: stuartbryson <email@hidden>
- Date: Mon, 6 Aug 2001 23:45:47 +1000
Hi all,
I am currently learning about Mem management in Cocoa. I currently have
a class called world with a NSArray *myArray declared in the interface
file. The object has 2 main methods; Build and Render. I need to alloc
the array within the Build method (as this is where the array is
populated), and then use the array in Render and release it. I am not
sure if there is a better way to achieve this?
Perhaps I can write my own init method for my world class where it
declares the array and a "destructor" (C++ I know) method where I can
release the array. That way the array would not need to be handled in
Build or Render, just used. Does this make sense?
Also when the array is released, do the objects within the array
automatically get released... see the code below:
- (void)Build
{
int i;
// Declare and create a temp 3D object and Ray
ThreeDObject *tempObj;
Ray *tempRay;
tempObj = [[Sphere alloc] init];
tempRay = [[Ray alloc] init];
// Init the 3D object array where all world objects are stored
threeDObjects = [[NSArray alloc] init];
// Add temp object
if ([self addThreeDObject:tempObj] == 0)
NSLog(@"Object could not be inserted");
// Add another temp object
if ([self addThreeDObject:tempObj] == 0)
NSLog(@"Object could not be inserted");
// Check to see intersects
for (i=0; i<[threeDObjects count]; i++)
if ([[threeDObjects objectAtIndex:i] intersects:tempRay])
NSLog(@"Ray intersected with Object");
[tempObj release];
[tempRay release];
}
Thanks all for your help.
Stuart