开发者

Variables get corrupted

I’m developing an iPhone App using Objective-C, based on Apple’s UIKit.

I’ve created some class instance in UIViewController and found my class instance pointer get corrupted randomly. Due to class instance pointer corrupted, I always get an EXC_BAD_ACCESS crash like this: Exception Type: EXC_BAD_ACCESS (SIGSEGV) Exception Codes: KERN_INVALID_ADDRESS at 0xa095633c Crashed Thread: 0

It shows that when my code is trying to access the class instance, it’s already turned into a ridiculous address:0xa095633c, which is beyond memory space and even binary imag开发者_JAVA百科e space.

Does any have any suggestion what kind of incorrect coding could possibly lead to class instance get corrupted?


There are a lot of things that can cause that. Just to name a few:

  • Accessing an already deleted object
  • Improper multithreading
  • Bugs in pointer arithmetic
  • Stack corruption
  • Going out of array's bounds

Learn to use debugger and debug logging to track down where and when the invalid pointer has appeared.

Try to create a small test program which reproduces the problem so you can post code example.


EXC_BAD_ACCESS usually indicates a memory management error. Read the Memory Management Programming Guide and make sure you conform to all the rules about alloc/retain/release/etc.


Did you try to retain the object when assigning to a variable?

A quick example:

variable = [[[UIViewController alloc] init] retain];

Another solution could be to declare an instance variable as property with the attribute "retain":

@property(nonatomic, retain) UIViewController *variable;


The most likely cause of this kind of error is the object getting deallocated prematurely and some other memory is getting written in its place. Try turning on Zombies in Instruments and it will tell you if this is the case.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜