-2

I have an array of employee objects. Whenever I print them using NSLog(), the array gets logged but it starts to deallocate the employee objects though I have not assigned the objects to nil. Here is my Employee.h :

@interface : Employee:NSObject
{
NSMutableArray *assetsarray;
}
@property int ID;

-(void)dealloc;
-(NSString *)description;

Employee.m :

@synthesize ID;

    -(void)dealloc
    {
    NSLog(@"Deallocating Employee %i" , self.ID);          
    }

    -(NSString *)description
    {
    return [NSString stringWithFormat:@"Employee %i" , self.ID];      
    }

Here is my main.m : (Please ignore the comments)

 int main(int argc, const char * argv[])
{

          @autoreleasepool {

                    NSMutableArray *employeesarray = [NSMutableArray array];
//                    NSMutableArray *assetsarray = [NSMutableArray array];

                    for (int i = 1; i < 11; i++) {

                              Employee *emp = [[Employee alloc]init];

                              [emp setID:i];

                              [employeesarray addObject:emp];

                    }

//                    
//                    for (int j = 1; j<11; j++) {
//                              
//                              
//                              Asset *ass = [[Asset alloc]init];
//                              
//                              ass.label = [NSString stringWithFormat:@"Laptop %i" , j];
//                              ass.resaleValue = j*18;
//                              
//                              [assetsarray addObject:ass];
//                              
//                              int index = arc4random() % [employeesarray count];
//                              
//                              
//                              [[employeesarray objectAtIndex:index] addAssetsObject:ass];
//                              
//                              
//                    }

                    NSLog(@"%@" , employeesarray);




          }
    return 0;
}

Here is the output :

2014-03-18 20:45:16.456 Employee[2112:303] (
    "Employee 1",
    "Employee 2",
    "Employee 3",
    "Employee 4",
    "Employee 5",
    "Employee 6",
    "Employee 7",
    "Employee 8",
    "Employee 9",
    "Employee 10"
)
2014-03-18 20:45:16.458 Employee[2112:303] Deallocating Employee 1
2014-03-18 20:45:16.458 Employee[2112:303] Deallocating Employee 2
2014-03-18 20:45:16.458 Employee[2112:303] Deallocating Employee 3
2014-03-18 20:45:16.459 Employee[2112:303] Deallocating Employee 4
2014-03-18 20:45:16.459 Employee[2112:303] Deallocating Employee 5
2014-03-18 20:45:16.459 Employee[2112:303] Deallocating Employee 6
2014-03-18 20:45:16.460 Employee[2112:303] Deallocating Employee 7
2014-03-18 20:45:16.460 Employee[2112:303] Deallocating Employee 8
2014-03-18 20:45:16.460 Employee[2112:303] Deallocating Employee 9
2014-03-18 20:45:16.460 Employee[2112:303] Deallocating Employee 10
Program ended with exit code: 0
methi1999
  • 25
  • 5
  • 1
    Show more code from main.m, especially what happens with `employeesarray` after the loop – Wojtek Surowka Mar 18 '14 at 15:25
  • Could you show all the test code with the NSLog code? – simalone Mar 18 '14 at 15:25
  • What you show above appears to be perfectly normal. When `employeesArray` passes out of scope without being stored somewhere else it will be deallocated. And when it is deallocated the entries in it will be deallocated, since they have no other reference. – Hot Licks Mar 18 '14 at 15:35
  • (Note that `NSMutableArray *employeesarray =...` declares a new pointer, separate from any more-global pointers there may be by the same name. Further, `[NSMutableArray array]` creates an autoreleased instance which will be released when it passes outside of boundaries of the `@autorelease` block. So both the pointer is going out of scope and the inherent life of the object is ending. Unless the object is referenced by another strong pointer it will be deallocated.) – Hot Licks Mar 18 '14 at 15:41
  • @HotLicks: "Further, [NSMutableArray array] creates an autoreleased instance" This is not guaranteed. But either way it is still true that the array will be deallocated at the end of the autoreleasepool block. – newacct Mar 19 '14 at 08:19
  • @newacct - Prior to ARC it produced an autoreleased array. With ARC it does whatever ARC wants, but the effect is the same. – Hot Licks Mar 19 '14 at 11:57

1 Answers1

0

The behaviour you see is normal. The array deallocates its elements because it is deallocated itself at the end of the block. Did you expect the objects to be never deallocated?

Wojtek Surowka
  • 18,864
  • 4
  • 43
  • 48
  • But why can't I keep the array in memory? What should I do if I WANT to keep it in memory? I have to extend the program using employee assets,etc. – methi1999 Mar 18 '14 at 15:33
  • The answer is not entirely correct (or perhaps a bit misleading). The array is an autoreleased object and therefore released when program control leaves the scope of the `@autoreleasepool`. - When `main()` ends, all memory is simply reclaimed by the system (without calling `dealloc`). There can be no memory leak when a program has terminated. – Martin R Mar 18 '14 at 15:35
  • The array is kept in memory till your program finishes. You cannot "keep" it in memory after the program ended. However, try to do anything with your array and you will see that it still exists and you can operate on it. Only after your instructions it will be deallocated. – Wojtek Surowka Mar 18 '14 at 15:36
  • To keep the array you must keep a pointer to it. Once you exit the above code there is no other pointer to `employeesArray`, so you couldn't reference it if you wanted to. – Hot Licks Mar 18 '14 at 15:36