Android ndk NULL pointer causes crash Problems

Source: Internet
Author: User

1. After the app is started, crash is randomly triggered, and the captured logs are as follows:

 
---------------- 2013-06-03 10:26:52 ---------------- #00 PC 0002e9b4/data/COM. XXXX. MAP/lib/libmapengine. so #01 LR 8082dc97/data/COM. XXXX. MAP/lib/libmapengine. so

2. Crash in assembly code2e9b4Location: generate the Assembly file corresponding to the so package using the objdump tool to locate the location of the crash:

 
0002e9b4 <_ zn12texturecache15ensurecachesizeei>: 2e9b4: 6803 ldrr3, [r0, #0] 2e9b6: 428b cmpr3, R1 2e9b8: da00 Bge. <_ zn12texturecache15ensurecachesizeei + 0x8> 2e9ba: 6001 strr1, [r0, #0] 2e9bc: 4770 bxlr

Location 2e9b4 of the first line.

3. source code of the corresponding ensurecachesize function:

 
Void texturecache: ensurecachesize (INT size) {If (Limit <size) {Limit = size ;}}

After seeing the function source code, I was a little confused. The function had an if judgment internally. Why is the crash? 2e9b4 is a LDR addressing operation. It is known from the Assembly source code that it is the limit member variable value.

4. I discussed it with an experienced colleague. He said this is a typical crash problem caused by ground null pointers. The reasons are as follows:

This function is called as follows: tiletexcache-> ensurecachesize (blknum + 1 );When the tiletexcache pointer is null, this line of crash is not called by the function, but inside the member function.

The principles in the C ++ Object Model Book are described as follows: the member functions of classes in C ++ are not the same as those of member variables. member variables are defined within objects at a level similar to objects, it is related to the this pointer, while the member function is defined inside the class and has a level with the class and has nothing to do with the this pointer. To understand this sentence from another perspective, even if the object pointer is null, it is also possible to access the member function through the Null Object Pointer. Crash is only available when the member function needs to access the member variable, because the member variables are defined inside the object. I understand this, but how can it be associated with a null pointer!

5. Test demo

 
Class emptypointerapp {public: void safefunc () {cout <"World Peace" <Endl;} void badfunc () {cout <"will crash" <Endl; mdata = 1;} PRIVATE: int mdata;}; int main (INT argc, char ** argv) {emptypointerapp * psample = NULL; psample-> safefunc (); psample-> badfunc ();}

Vs2008, crash in the mdata = 1 Statement, the output at this time:

World peacewill crash

Open the Disassembly window after debugging is interrupted:


The first mov willThisThe pointer content is put in the eax register. dword ptr indicates that the unit in which the command accesses the memory is a DWORD, that is, four bytes in length.

When the second mov accesses the mdata member through eax, crash!

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.