Resolve memory out-of-bounds access issues on iOS

Source: Internet
Author: User

iOS often mixes C code, while in C, reading and writing to memory is a very frequent operation.

Where memory is out-of-bounds read and write

    unsigned char* p = (unsigned char*) malloc (+);    unsigned char  c = * (p+100);    Free (p);

is a very easy mistake, and in most cases this code will not expose the problem.

If you write memory out of bounds, such as:

    unsigned char* p = (unsigned char*) malloc (10); * (p+100) = ' C ';    Free (p);

Like what:

    unsigned char* p = (unsigned char*) malloc (10); uiviewcontroller* VC =[uiviewcontroller new];     * (p+100) = ' C '; [VC view];    Free (p);

Because violence changes the contents of VC objects, and then the VC objects to operate, there will be an exception occurs.

In reality, such bugs are often hidden deep. Because of the memory that is modified over the bounds, it may belong to an unused memory, or to an object that is infrequently accessed. For testers, the application will become unstable, under any page, it is possible to hang, and there is no necessary connection between them. Like burying a mine on the road, anyone could have stepped on the Thunder.

Fortunately, Xcode provides a way for us to correct this kind of bug.

1, at run time, add the following options:

This method applies only to simulators.

If you are debugging on a real machine, you will not find a mirror-phase error.

2, when the above memory read and write code, will directly navigate to the code of the problem.

Resolve memory out-of-bounds access issues on iOS

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.