HML
HML

Reputation: 103

Can NSLog crash application?

I am new to iPhone SDK.I am printing each and every array in console.No matter how much data consume.and I note that some times my application is crashing.it gives me BAD_ACCESS and pointer showing on my NSLog line.I am confused can NSLog crash the application? Here is my code :

for (int i = 0; i < [UserNeedListArray count]; i++) {
    EndUserNeed* aEndUser = [UserNeedListArray objectAtIndex:i];
    if ([appData.CurrentUser.userId isEqualToString:aEndUser.UserId]) {
        NSMutableArray* temp = [[NSMutableArray alloc]init];
        [temp addObject:aEndUser];
        NSLog(@"%@",temp);
        [arr_ShowMyOnly addObject:[temp objectAtIndex:0]];
        NSLog(@"%@",arr_ShowMyOnly);
        [temp removeAllObjects];
        [temp release];
        temp = nil;
    }
}

Please help me.Thanking you...

Upvotes: 1

Views: 2253

Answers (5)

Dipen Chudasama
Dipen Chudasama

Reputation: 3093

Try this my be help you.

for (int i = 0; i < [UserNeedListArray count]; i++) {
        EndUserNeed* aEndUser = [UserNeedListArray objectAtIndex:i];
        if ([appData.CurrentUser.userId isEqualToString:aEndUser.UserId]) {
            NSMutableArray* temp = [[NSMutableArray alloc]init];
            [temp addObject:aEndUser];

            NSLog(@"%@",temp);
            if([temp count] >0)
            {
               [arr_ShowMyOnly addObject:[temp objectAtIndex:0]];
            }
            if([arr_ShowMyOnly count] >0)
            {
               NSLog(@"%@",arr_ShowMyOnly);
            }
            [temp removeAllObjects];
            [temp release];
            temp = nil;
        }
    }

Upvotes: 0

Paresh Navadiya
Paresh Navadiya

Reputation: 38239

As arr_ShowMyOnly is allocated i assume, so do this:

  for (int i = 0; i < [UserNeedListArray count]; i++) 
  {
    EndUserNeed* aEndUser = [UserNeedListArray objectAtIndex:i];
    if ([appData.CurrentUser.userId isEqualToString:aEndUser.UserId]) 
    {
      [arr_ShowMyOnly addObject:aEndUser];
      NSLog(@"%@",arr_ShowMyOnly);
    }
  }

Upvotes: 0

CarlJ
CarlJ

Reputation: 9481

some more NSLog() example:

NSString *format = @"%s %d";    
NSLog(format, __FUNCTION__);

Would crash on a device and wouldn't crash on the simulator

Upvotes: 5

Alex Terente
Alex Terente

Reputation: 12036

Yes it can. when you do NSLog(@"%@",temp); it expects that temp is a type of NSString class.

In your case you should do NSLog(@"%@",[temp description]); Almost all the classes have a description method that will return you an NSString object.

Upvotes: -1

Morion
Morion

Reputation: 10860

As the Ricard said, problem with your arr_ShowMyOnly variable , not with NSLog. Check where you retain and release it. Check if it was retained after creation< for example. By the way, you can add code of arr_ShowMyOnly initialilzation

Upvotes: 0

Related Questions