How I use the function string_sprintf()?
If I modify the code on how I use that function then there is no crash, so it makes me conclude that maybe the crash has nothing to do with MALLOC()/FREE(). My new questions are:
- What is causing the crash on vsnprintf(buf, bufsize, format, args); in my first code?
- What would you do if you have a problem like this? Do you believe that the crash maybe caused by Android Studio bugs, and if so, then would you leave like how I slightly change the code then the crash seems to be gone without being sure if it's gone for real (there is no logic) which means that there maybe another hidden crash somewhere. "A bug like that is one of the most difficult bugs to detect because its cause is unknown".
// Event Type
...
enum EventType {
...
};
...
inline string EventType_postfix(EventType x) { // postfix vs suffix: https://wikidiff.com/postfix/suffix
... (same as EventValue_postfix())
}
// Event Value
enum EventValue {
EventValue_None = 0,
EventValue_Down,
EventValue_Up,
EventValue_Click,
EventValue_DoubleClick,
EventValue_Move,
EventValue_Cancel,
};
inline string EventValue_postfix(EventValue x) { // postfix vs suffix: https://wikidiff.com/postfix/suffix
#define MACRO_ITEM(x) case EventValue_##x: return #x;
switch (x) {
MACRO_ITEM(None)
MACRO_ITEM(Down)
MACRO_ITEM(Up)
MACRO_ITEM(Click)
MACRO_ITEM(DoubleClick)
MACRO_ITEM(Move)
MACRO_ITEM(Cancel)
default:
return "???";
}
#undef MACRO_ITEM
}
void _Window::_event_pointer(const WindowPointer &pointer, bool wantCaptureMouse) {
#if 1
// FIXME: There's a crash only with Pixel_3a_API_33_x86_64 (Android Studio 2021.3.1 Patch 1)
// and on emc-engine 4.217.
// Maybe the cause of the crash is on MALLOC()/FREE(), or maybe it's somewhere else in the
// project, that's why the other code for trying to find the cause of the crash.
string s = string_sprintf(
"{id:%d, pos:(%.1f,%.1f), type:%s, value:%s}\n",
pointer.id,
pointer.pos.x,
pointer.pos.y,
EventType_postfix(pointer.type).c_str(),
EventValue_postfix(pointer.value).c_str());
#else // This code doesn't crash, but it looks exactly the same as above.
string s1 = EventType_postfix(pointer.type);
string s2 = EventValue_postfix(pointer.value);
string s = string_sprintf(
"{id:%d, pos:(%.1f,%.1f), type:%s, value:%s}\n",
pointer.id,
pointer.pos.x,
pointer.pos.y,
s1.c_str(),
s2.c_str());
#endif
g_debug_pointerEventsLog.push_back(s);
if (g_debug_pointerEventsLog.size() > 5) {
g_debug_pointerEventsLog.erase(g_debug_pointerEventsLog.begin());
}
...
}