We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 4133 - SDL_GetBasePath() causes a crash on Android
Summary: SDL_GetBasePath() causes a crash on Android
Status: NEW
Alias: None
Product: SDL
Classification: Unclassified
Component: file (show other bugs)
Version: 2.0.8
Hardware: ARM Linux
: P2 normal
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2018-04-13 04:39 UTC by Steve Robinson
Modified: 2018-04-13 04:39 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Steve Robinson 2018-04-13 04:39:21 UTC
On Android, I had the following line in the private data of my 'Game' object:
std::string basePath = SDL_GetBasePath();

It's used to define an absolute path to assets (in this case, a font file) so that if someone opens the program from a different directory on Linux, it still finds the assets.

It worked fine on Linux, Windows, and Mac, but crashed at runtime with this call stack:
04-12 20:42:08.845 16863-16902/com.my.app V/SDL: Running main function SDL_main from library libGame.so
04-12 20:42:09.105 16904-16904/? A/DEBUG:     #00 pc 00018788  /system/lib/libc.so (strlen+71)
        #01 pc 00051ffb  /data/app/com.my.app-2/lib/arm/libGame.so (_ZNSt6__ndk111char_traitsIcE6lengthEPKc+10)
        #02 pc 0004e6db  /data/app/com.my.app-2/lib/arm/libGame.so (_ZN4GameC1EiiPKcb+246)

Looks like the function is defined for Android here:
/src/filesystem/android/SDL_sysfilesystem.c

Full function text:
char *
SDL_GetBasePath(void)
{
    /* The current working directory is / on Android */
    SDL_Unsupported();
    return NULL;
}


In an attempt to fix this, I followed the comment above and on Android simply set:
std::string basePath = "/"
Which didn't work, but a blank string did.

So my final workaround was this:
#ifdef __ANDROID__
    std::string basePath = "";
#else
    std::string basePath = SDL_GetBasePath();
#endif

I think SDL_GetBasePath(void) should return a blank string.  I suppose that could be misleading if someone is trying to use this function to actually find out where in the directory structure the apk is located, but it would work for my purpose.

Or, you could leave the function undefined on Android and it would be a very visible and obvious build error.  That would've made this problem far easier to track down.