views:

53

answers:

2

I'm trying to solve my first really hard EXC_BAD_ACCESS problem. I see from a lot of tutorials and blogs that I can use Zombies to help me figure out where I'm going wrong. But I don't think my zombies are working, and I'm DEFINATELY not getting anything useful out of the console.

When I start the program I see this: This GDB was configured as "x86_64-apple-darwin".Setting environment variable "NSZombieEnabled" to null value.

Eventually, I see a couple of NSLog traces I used and then:

Program received signal:  “EXC_BAD_ACCESS”.

No help at all. Any ideas? I have a sneaking suspicion that even with the zombies working it won't be able to tell me where the bad access happened. Objective C makes me sad--This would have taken 3 seconds in ActionScript :(


You say the crash is here (edit the question next time):

- (IBAction) toggleView{
if(switchableView.subviews.count != 0)
    [[switchableView.subviews objectAtIndex:0] removeFromSuperview];
UIViewController* newView = (viewSelector.selectedSegmentIndex == 0) ? [Login new] : [UserRegistration new];
[switchableView addSubview:newView.view];
//[newView release];
}
A: 

There is no reason why this error has to be caused by an over-released object. There are plenty of other reasons why you might be crashing.

Program received signal: “EXC_BAD_ACCESS”.

OK -- so where is it crashing? What is the backtrace? It should have crashed to the debugger and the debugger should be showing you a bunch of details about the crash.

Post the backtrace and any of your code that happens to be in that backtrace.


That isn't a backtrace, but it does give some more context. A backtrace would be helpful in that it'll show exactly where the crash is happening.

As far as zombies are concerned, I find it easier to use the Run -> Run With Performance Tool -> Zombies menu item to fire up Instruments in Zombie detection mode.

That the crash goes away when you comment out the release certainly indicates a memory management problem. The code you posted looks OK, under the assumption that you didn't override new.

Have you tried "Build and Analyze"?

That you are getting an unexpected table indicates that there are [obviously] other problems in your code. That might also contain memory management issues, too?

Also -- if the removeFromSuperview: causes the view that is later queried for selected segment to release & deallocate, that could cause the crash, too.


Objective C makes me sad--This would have taken 3 seconds in ActionScript

Anything you don't know makes you sad when you try to treat it like something unrelated that you do know. Success with any language, tool, or new environment is as much about your attitude as it is the details of the tool.

I'd suggest you take a step back, do a few tutorials and/or read some documentation about debugging applications.

bbum
Here's the place where it crashes. I'm trying to load a table into a subview. It worked when I was loading a regular view controller-- not a table.
pseudopeach
(IBAction) toggleView{ if(switchableView.subviews.count != 0) [[switchableView.subviews objectAtIndex:0] removeFromSuperview]; UIViewController* newView = (viewSelector.selectedSegmentIndex == 0) ? [Login new] : [UserRegistration new]; [switchableView addSubview:newView.view]; //[newView release];}
pseudopeach
If I comment out the release, it loads a table, but not really the one I expect. If I don't release the object, isn't it a memory leak?I read this:http://stackoverflow.com/questions/937767/does-uiviews-addsubview-really-retain-the-view , but I don't really understand the solution
pseudopeach
+2  A: 

Actually you are getting something useful from the console. It's telling you that you haven't enabled NSZombie - "Setting environment variable "NSZombieEnabled" to null value."

The value needs to be YES not null.

Executables -> Your Target Name -> Arguments -> Variables to be set in the environment

Even without NSZombies enabled it should be very straightforward to see where your bad access is from your call stack. Action script is a scripting language.

Nick
Thanks for that, I guess I thought the checkmark was a reasonable representation of yes. I found my problem, but I can't understand the solution.
pseudopeach
It's not always easy to find, due to the magic of autorelease. When those bite, the backtrace isn't always helpful.
Ben Scheirman
Here's what does help. Use Instruments, run with Object Alloc. Stop your app, then press the small "i" in the ObjectAlloc instrument, and enable NSZombie detection along with reference counts. Then press "record", your app will run (do this in the simulator) and when you get the "bad access" it will flag in the Instruments tool with an arrow - press that and you'll see each retain and release against that object.
Kendall Helmstetter Gelner