[Switch] protobuf2.5.0 on <Delete [] elements _;> crash.

Source: Internet
Author: User
Background

Protobuf is used as the network transmission protocol in the project, and source code compilation is used directly in the project at the beginning. It is normal to test on a real machine, it was not until a 64-bit CPU device found that protobuf caused crash one day, so it began to try again. A static database to see if the problem can be solved (failed ). The bug solution goes straight to the end.

Development Environment
  • OS X 10.9 mavericks
  • Xcode 5.0.2
  • Protobuf 2.5.0
  • Ios7
Crash Phenomenon

When protobuf is statically initialized, crash is in a Delete [] xxxptr; the pointer is null. It is very strange that on a 32-bit iOS device, it is also executed here as well as Delete [] nullptr. After a simple search, there was nothing to gain, so I gave up and solved the problem directly.

[CPP]View plaincopy
  1. Template <typename typehandler>
  2. Void repeatedptrfieldbase: Destroy (){
  3. For (INT I = 0; I <allocated_size _; I ++ ){
  4. Typehandler: delete (cast <typehandler> (elements _ [I]);
  5. }
  6. Delete [] elements _; // elements _ = NULL
  7. }
Try to compile.

Slightly too much because of xcode upgrade and invalid/need to modify the method, directly on the current find the most reliable automatic Script: https://gist.github.com/BennettSmith/7150245

This script implements:

  • Download protobuf 2.5.0 source code
  • Production of static libraries on five platforms (MAC 64, iPhone simulator, armv7, armv7s, and arm 64)
  • Merged into a complete universal static library.

Compilation is successful. For most people, it may end here.

But... The Link error not found in the STD library still occurs when the generated. A is added to the project.

Static library edited

After adjusting the cxx compilation parameters except for the mac64 platform and matching the parameters used in the project, we finally succeeded.

The final parameters used are as follows (mac64 remains unchanged)

    • Cxx = clang ++
    • Cxxflags_arm = "$ {cflags}-stdlib = libstdc ++"
    • Ldflags_arm = "-stdlib = libstdc ++"

But... The 64-bit device is still crash. You can announce that this attempt failed!

Problem Solving
Unexpectedly, the crash is libproto. dylib, which is provided by Apple and is not used in the project. Google: "imageloader on the newer devices (iPhone 5S and apparently iPad Air) has its own copy of protocol buffers which causes symbol collisions. ", that is to say, there is a library with a protobuf on the latest three devices, which is the same as the namespace of protobuf in the project. This creates a conflict. The solution is simple:
  1. Use protobuf source code (the source code is used for this project)
  2. Modify Google \ protobuf \ stubs \ common. H, add a macro definition, and modify the local namespace.
[CPP]View plaincopy
  1. Namespace STD {}
  2. # Define Google google_private
  3. Namespace Google {
  4. Namespace protobuf {
Related information some default parameters of xcode 5.0.2
  • C language Dialect: gun99 (-STD = gnu99)
  • C ++ language Dialect: GNU ++ 98 (-STD = GNU ++ 98)
  • C ++ standard library: libstdc ++ (-stdlib = libstdc ++)

The answer on stackoverflow: http://stackoverflow.com/questions/19848118/weird-ios-libprotobuf-dylib-cause-crash

[Switch] protobuf2.5.0 on <Delete [] elements _;> crash.

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.