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
- Template <typename typehandler>
- Void repeatedptrfieldbase: Destroy (){
- For (INT I = 0; I <allocated_size _; I ++ ){
- Typehandler: delete (cast <typehandler> (elements _ [I]);
- }
- Delete [] elements _; // elements _ = NULL
- }
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:
- Use protobuf source code (the source code is used for this project)
- Modify Google \ protobuf \ stubs \ common. H, add a macro definition, and modify the local namespace.
[CPP]View plaincopy
- Namespace STD {}
- # Define Google google_private
- Namespace Google {
- 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.