Another reason for the EXC_BAD_ACCESS class in iOS development is iosexc_bad_access.

Source: Internet
Author: User

Another reason for the EXC_BAD_ACCESS class in iOS development is iosexc_bad_access.

Today, when I happened to learn about iOS development, I encountered an exception in EXC_BAD_ACCESS. The information I found is explained by the fact that the heap memory object has been recycled. For details, refer:

Http://code.tutsplus.com/tutorials/what-is-exc_bad_access-and-how-to-debug-it--cms-24544

However, my example is not caused by this reason. Please refer to the example:

 1 #import <Foundation/Foundation.h> 2  3 @interface MyRectangle : NSObject 4  5 -(void) setWidth: (int) width; 6  7 -(void) setHeight: (int) height; 8  9 -(void) setWidth:(int)width andHeight: (int) height;10 11 -(int) area;12 13 @end14 15 // Implemention16 17 #import "MyRectangle.h"18 19 @implementation MyRectangle {20     int width;21     int height;22 }23 24 -(void) setWidth: (int) w {25     width = w;26 }27 28 -(void) setHeight: (int) h {29     height = h;30 }31 32 -(void) setWidth:(int)w andHeight: (int) h {33     width = w;34     height = h;35 }36 37 -(int) area {38     return width * height;39 }40 41 @end

Main Function

1 # import <Foundation/Foundation. h> 2 # import "MySquare. h "3 4 int main (int argc, const char * argv []) {5 @ autoreleasepool {6 // reflection 7 MyRectangle * rect = [[MyRectangle alloc] init]; 8 [rect setWidth: 10]; 9 [rect setHeight: 20]; 10 11 id x_id = [rect example mselector: @ selector (area)]; 12 NSLog (@ "selector got area is % @", x_id); 13} 14 return 0; 15}

The reason is that area returns an int type of data. The int type is actually a value placed in the stack memory and assigned to the id variable of the type. The id variable means: the compiler treats it as any object type. In this case, the value obtained by the area Method during operation is returned to x_id, and then the value is used as the heap memory address. Of course, this data cannot be found here, this will cause access failure errors.

From the above example, we can see that the exception of EXC_BAD_ACCESS means that the address value in the memory cannot be accessed, probably because some variables have been recycled, it may also be because the data of the basic type of stack memory is assigned to the id type variable.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.