A bug caused by carriage return

Source: Internet
Author: User

Two days ago solve a cocos2d-x parsing XML error bug (link here), full interesting, documented.

Confused

The problem is: When an engine user posts on a forum that the engine runs a test example of game data in XML format on Windows 7ProgramCrash.

I am confused when I see this question. We have tested this test many times before the release. We ran it on Windows, on Mac, on PC, and on mobile phones, running on the iPhone and running well on andorid, what's the problem?

Exhausted

Do not guess, or debug first. Well, the company's development environment is only Windows XP, and there is no Windows 7. Since it is all windows, run it on XP first. CheckoutCode... Compile... Run... Run the test example... Everything is normal ......

In the evening, this is Windows 7. Come back... Checkout code... Compile... Run... Run the test example... Hung up. Haha, just reproduce it. Start debugging, and debugging, huh? Libxml2 parsing failed? Is the XML file checkout on Windows 7 different from that of others? Use VC to open the XML file, VIM to open the XML file, debug, open the variable debugging window, and open the memory monitoring window. I can look at it on the left and see it. Each letter has a sample.

What is going on?

Liu yinghuaming

According to the principle, libxml2 should not be a problem with such a popular library. Flip the Code:

 
1:......

2:File * fp = fopen (pfilepath,"R");// Open the file

 
3:Fseek (FP, 0, seek_end );// To the end of the file

 
4: UnsignedNsize = ftell (FP );// Get the object Length

 
5:Fseek (FP, 0, seek_set );// Return the file header

6: Unsigned Char* Pbuffer =New Unsigned Char[Nsize];// Allocate data cache

 
7:Fread (pbuffer,Sizeof(Unsigned Char), Nsize, FP );// Read the file content

 
8:......

9:Xmlsaxuserparsememory (& saxhandler,This, Pbuffer, nsize );// Parse the File Content

 
10:... // Have you found any problems?

It seems that there is no problem? Continue debugging... Download the libxml2 source code... Compile the debug version... Replace the library in the project... One-step debugging to parse XML file data... From the first sentence of parsing to the last sentence... No problem... Slow! Both

After the data is parsed, how can I continue to read the data in the cache? After the data is parsed, access continues.

Enter the variable name (pbuffer, 1117) in the variable monitoring window of vc ide for a detailed observation (by the way, 1117 is the number of file bytes returned by ftell )... I finally found the problem:

The number of bytes of the XML file returned by ftell is 1117, but the number of bytes in pbuffer to more than 9th characters is the end of the file content, followed by the memory not assigned after the allocation, the data is all uncertain values, and libxml2 cannot be parsed later.

Late conclusion

Why does ftell return 1117 bytes while fread only reads more than 9 hundred characters? Use Vim to open the XML file, convert it to hexadecimal format, and check it carefully. The problem is: Press enter. Well, it is too troublesome to explain it using the debugging example. Let's simply put it:

For example, there is a text file with 11 characters (not counted as the end 0) in the string "ABCDE \ n12345". It occupies 11 bytes in the memory and we put it in the text file, how many bytes is the data in this text file?

Both 11 and 12 bytes are possible, because the carriage return in the file may be "\ n" (Linux), "\ r" (MAC) or "\ n \ r" (Windows ). When fread reads text files in "RT" mode on Windows, it automatically converts "\ n \ r" to "\ n ".

Therefore, according to the Code logic above: according to the length of the file returned by ftell, a 12-byte cache is allocated. fread reads the entire file data and fills in the first 11 bytes, the data to be parsed by libxml2 is "ABCDE \ n12345?" (? Is not assigned a value for the memory), when it is parsed? Failed to parse. Find the cause and solve the problem simply:

1:Nsize = fread (pbuffer,Sizeof(Unsigned Char), Nsize, FP );

Compile... Run the test example... Pass... Close.

 

 

Why is it okay to run on Windows XP?

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.