views:

50

answers:

2

Hi all,

I'm absolutely stumped at this. My properly working application [on iOS<=3.1] was crashing on iOS4. When I tried debugging it, I saw it's crashing at this line:

EDIT: I tried changing the position of this statement and put some log statements to check and I could confirm that the application is crashing just at this line.

This is a part of my code:

    //Various allocations here like

lblForAttachingPhoto = [[UILabel alloc]initWithFrame:CGRectMake(235, 105, 40, 20)];
lblForAttachingPhoto.backgroundColor = [UIColor grayColor];
[lblForAttachingPhoto setText:@"140"];
[lblForAttachingPhoto setTextAlignment:UITextAlignmentCenter];

btnAttachPhoto = [UIButton buttonWithType:UIButtonTypeCustom];
btnAttachPhoto.frame = CGRectMake(275, 105, 20, 20);
btnAttachPhoto.autoresizesSubviews = YES;
btnAttachPhoto.clipsToBounds = YES;
[btnAttachPhoto setImage:[UIImage imageNamed:@"arrow.jpg"] forState:UIControlStateNormal];
[btnAttachPhoto addTarget:self action:@selector(attachPhoto) forControlEvents:UIControlEventTouchUpInside];

viewForAttaching = [[UIView alloc]init];
viewForAttaching.frame = CGRectMake(5, -340, 310, 140);


for(NSInteger e=0;e<[_appDelegate.genericArray count];e++)
{
    [genericArray addObject:[_appDelegate.genericArray objectAtIndex:e]];
}

for(NSInteger f=0; f< [_appDelegate.category_type count]; f++)
{
    [category_type addObject:[_appDelegate.category_type objectAtIndex:f]];
}

txtVw = [[UITextView alloc]init];     //txtVw is a textview

But despite the position of the statement of textview allocation statement, it's making the app crash.

Console output:

          Program received signal:  “EXC_BAD_ACCESS”.
          warning: check_safe_call: could not restore current frame

          warning: Unable to restore previously selected frame.

Is it possible? I'm also implementing UITextViewDelegate protocol.

Can anybody please help?

Thanx in advance.

A: 

You need to retain the txtVw variable. The problem is that by the time your code gets to assigning an object to txtVw the application has "forgotten" it. Try this to fix it. In your init method do:

txtVw = [[UITextView alloc]init];
[txtVw retain];

if you don't declare it as a property then it won't retain the variable and will forget it very quickly.

I will assume this is your problem. if it doesn't work then tell me and I'll have another look.

Thomas Clayson
+alloc has already retained txtVw. There's generally no reason to additionally retain it here. If you are assigning an object generated with +alloc to a retained ivar, you need to also autorelease it or you will leak.
Rob Napier
+1  A: 

It's unlikely that the crash is actually due to the UITextView initialization. I actually suspect the line above it in the for() loop. You have very inconsistent naming and memory management, and that is dangerous in ObjC.

First, give your variables consistent names. category_type sounds like an enum, but it appears to be an NSMutableArray. This kind of naming will kill you in ObjC; you have to keep track of what things are because the compiler often won't catch type mismatches. You should have a name like categoryTypes.

Next, you should not access your ivars directly. Always use accessors. Always. (Except possibly in dealloc; there's controversy there.) This is the #1 way to avoid EXC_BAD_ACCESS. You are clearly not retaining something correctly. Accessors protect against that.

Do you really mean to append _appDelegate.genericArray to genericArray? Or do you really mean to make genericArray a copy of _appDelegate.genericArray? Or did you just want genericArray to point to _appDelegate.genericArray? If you want a copy, just copy it:

self.genericArray = [NSMutableArray arrayWithArray:self.appDelegate.genericArray];

I know all of this sounds like "just style," but consistent style is absolutely critical to stable ObjC. You're seeing the kinds of annoying crashes you get otherwise.

You should also hit "Cmd-Shift-A" and let the static analyzer look for bugs for you.

Rob Napier
Rob, I appreciate your help. Thanx for the nice explanation. But this' has not solved my issue. It's still crashing at the same line. And no matter where I write this line the program crashes there itself, like if I write it as a first statement in my viewDidLoad then it crashes there without processing any further statement. This' really frustrating.
neha
OK, break it down. Start with "[UITextView alloc]". Don't even assign it to a variable. Then try [[UITextView alloc] init]. Then try assigning "nil" to the variable. Then try assigning the textview to a stack variable and then assign the stack variable to the ivar (make sure to compile in Debug, not Release, so this isn't optimized out).
Rob Napier