-
Notifications
You must be signed in to change notification settings - Fork 7.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
v3.13 -> RapidJSON Crashes in Release mode on Android #16492
Comments
How about changing API-LEVEL to 10 and make sure API 10 is installed? |
Haven't tried api level 10 but it was working without any problems in previous cocos versions on API-LEVEL 11. I think it has something to do with gcc changing in 3.13. |
@ermanhaskan we just move back to gcc, only 3.12 uses clang. And how to reproduce it? |
rapidjson is used in |
NDK: r12b The first run was fine. Since then crash Fix :
}` |
@Finebe did you use simulator to run lua scripts? |
@ermanhaskan , I reproduced this issue now. Do you know the reason why it crashes in release mode? |
@dumganhar it's about memory allocation but have no idea why. |
UPDATED: Although I still don't know the reason, I just added an unused line to printf something at the following function: GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) :
allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_()
{
if (!allocator_) {
ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
printf("allocator: %p", allocator); // ADDED THIS LINE
}
} And everything works fine. |
@miloyip, do you have any idea about my above comment? |
@ermanhaskan, is older versions of cocos2d-x also trigger this issue? Or it's just happens in 3.13? |
I found that if we add |
@dumganhar this was not happening before 3.13 and I am using LOCAL_ARM_MODE := arm for years. |
@ermanhaskan , I reproduced this issue with the simplest code by modifying BTW, if you use |
@dumganhar how can I switch to clang? |
cocos2d-x/tools/cocos2d-console/plugins/plugin_compile/build_android.py def get_toolchain_version(self, ndk_root, compile_obj):
return '4.9' # return 'clang' to switch to 'clang' to compile But currently, So, you could test by modifying cocos2d-console to use And modify
-->
|
I have tried different configurations, and found that gcc 4.9.0 arm mode release configuration ( The current minimal reproducible code is simply: Document d;
d.Parse("[1]"); This will cause crash due to incorrect instruction emitted in So that, a workaround is to reduce optimization level to #include "rapidjson/rapidjson.h"
// Bug in gcc 4.9.0 on ARM with ARM instruction set, -O2 causes the bug
// This gcc version cannot use #pragma GCC push_options/pop_options as well. https://gcc.gnu.org/bugzilla/show_bug.cgi?id=59884
#if defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,9,0) && RAPIDJSON_GNUC < RAPIDJSON_VERSION_CODE(5,0,0) && defined(__arm__)
#pragma GCC optimize ("O1")
#endif
#include "rapidjson/document.h"
#if defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,9,0) && RAPIDJSON_GNUC < RAPIDJSON_VERSION_CODE(5,0,0) && defined(__arm__)
#pragma GCC reset_options
#endif This workaround only fixes on gcc 4.9.x and ARM. |
Hi @dumganhar, @minggo: Our game is crashing when ccs.load(...) is loaded in release mode, because it use rapidjson too. Will @miloyip's workaround be the final solution in cocos2d-x 3.14 or are you working on this issue? Thanks and best, |
Thanks @miloyip! Your workaround works fine. |
@miloyip can the compiling option be included in |
Hi: I've added this code in all CocosStudio classes where
It's working. |
@patriciog thanks for the information, but i think it is better to add it in |
I do not want to do such workaround in the master. This seem happen in some versions of compilers on some platforms, such kind of patching may make the code unclean. I suggest that, you may create a own wrapper header, which contains such patching and include the real header. And then replacing the existing usage of document.h with the wrapper header. |
@minggo ,PR was submitted here (cocos2d/cocos2d-x-3rd-party-libs-bin#254) |
@dumganhar why rename it to |
@dumganhar doing like this will be convenient for developers, but it is hard to update rapid json in future. What i mean doing the wrapper in cocos2d-x is that:
This method is good for upgrading, but should let developers know the new usage. |
Okay, so every developer should know to include Therefore,
IMO, we should make developer use rapidjson as before, i mean, use it like how rapidjson official teach. Yes, if @miloyip could apply the workaround into upstream, that will be best for us. I closed my PR for 3rd party library repo temporarily. |
Hi @dumganhar: I've created a wrapper following your steps for my game and it works: frameworks\changes\cocos2d-x\external\json\document-wrapper.h:
After I've replaced all instances of "#include "json/document-wrapper.h"" by "#include "json/document-wrapper.h" Thanks and best! |
@patriciog, thanks for testing. I will submit a Pull Request for this. |
PULL REQUEST DONE( #16792) |
My crash went away after I changed to using CRT Allocator, like this // Old Code // New Code |
After my game runs for the first time I'm getting this:
Then I'm getting this consistently, again and again.
Are those issues related to this bug? I'd highly appreciate any information. |
@rraallvv, it is probably the same issue. Please try the fix above. |
@dumganhar thanks for the info. I'll give it a try. |
FWIW, we just ran into the same issue on Windows, with release & debug builds alike, 100% reproducible. Merely switching to CRTDocument magically resolved our issues. We are building for MacOS, Ubuntu, Android and Windows. (and one week-end down the drain, hunting down some library bug... :-( ) |
@georges-berenger Is it possible to make a minimal reproducible example? |
Unfortunately, no. I've spent a nice chunk of time trying to make the issue happen in our unit tests, but I was unable to get anything there. The issue started happening after we made a very unrelated project change, but then, it would happen 100% of the time on the same very "innocent looking" rapidjson code after making a call to SetString(const char*, size, alloc) and returning the rapidjson::Value. BTW, thanks @jarsj for the idea of using CrtAllocator. This is a much more reasonable fix than disabling optimizations. Is there a chance that it could a symbol resolution issue between libraries? |
NDK: r12b
API-LEVEL: 11
Rapidjson's memory allocation functions crash every time when compiled in release mode. Fix is to use custom types instead of rapidjson::Document and rapidjson::Value.
The text was updated successfully, but these errors were encountered: