SDL main loop: how to access outer variable (struct)?

Hello,

I have a simple Android app. Native SDL part consists of 2 functions: update() and main(). update() is eventually called using JNI.

Code:

struct Word
{
char* source;
};

int currentCount = 0;
struct Word *currentWord = NULL;

void update(int count, struct Word *word) { //probably called from a different thread
currentCount = count;
currentWord = word;
SDL_Log(“Updated: currentWord->source=%s, currentCount=%i”, currentWord->source, currentCount); //verified that everything was set correctly
}

int main(int argc, char *argv[]) {
while(true) {
SDL_Delay(1000);
if(currentCount != 0) {
SDL_Log(“currentCount=%i”, currentCount); //outputs what expected
}
if(currentWord != NULL) {
SDL_Log(“currentWord=%s”, currentWord->source); // Fatal signal 11 (SIGSEGV) at 0xffffffff (code=1), thread 1162 (SDLThread)
}
}
}

When I do SDL_Log of currentCount (which is int), it outputs what is expected. When I do SDL_Log of currentWord->source, I get SIGSEGV error. Does it have anything to do with multithreading? How to make it print currentWord->source correctly inside the main() ?
I’m very new to SDL (and actually to C as well), so please forgive me if I do something obviously stupid.

Extract your currentword variable into a local var. The concurrency issue
might be (or one of them) that between the check of the global var and
accessing it, another thread might have changed it already.

Another issue might be, that the word memory is freed by another thread
already. When you do free(word) you should also set the currentword to null
if that was the freed one. But all this must be locked - because its not
atomic and you might run into race conditions when you dont use locks i
your code. Sdl has everything you need to make this work. Check out the
sdl_thread.h (it was called something like that).

Never do things like:
If (globalvar == 42) { dosomething (globalvar); }

always extract to local var before. If you use a pointer as global var and
a thread might change the members of the underlying struct, this will not
work of course.

Did you define currentword as extern somewhere? If not, make it static.
Static is the c way to say that the variable is compilation unit private
and it is automatically initialized with zero/null.Am 13.05.2014 21:24 schrieb “denisk” <denis.k1985 at gmail.com>:

Hello,

I have a simple Android app. Native SDL part consists of 2 functions:
update() and main(). update() is eventually called using JNI.

Code:

struct Word
{
char* source;
};

int currentCount = 0;
struct Word *currentWord = NULL;

void update(int count, struct Word *word) { //probably called from
a different thread
currentCount = count;
currentWord = word;
SDL_Log(“Updated: currentWord->source=%s, currentCount=%i”,
currentWord->source, currentCount); //verified that everything was set
correctly
}

int main(int argc, char *argv[]) {
while(true) {
SDL_Delay(1000);
if(currentCount != 0) {
SDL_Log(“currentCount=%i”, currentCount); //outputs what expected
}
if(currentWord != NULL) {
SDL_Log(“currentWord=%s”, currentWord->source); // Fatal signal 11
(SIGSEGV) at 0xffffffff (code=1), thread 1162 (SDLThread)
}
}
}

When I do SDL_Log of currentCount (which is int), it outputs what is
expected. When I do SDL_Log of currentWord->source, I get SIGSEGV error.
Does it have anything to do with multithreading? How to make it print
currentWord->source correctly inside the main() ?
I’m very new to SDL (and actually to C as well), so please forgive me if I
do something obviously stupid.


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

@M. Gerhardy thanks for your suggestions.
Extracting currentWord into a local var does not help. There must be indeed a multithreading issue since I’m trying to share a pointer to the Word structure between 2 threads.
If I add SDL_Delay to update() function then it prints currentWord correctly for about 5 seconds. After update() function exits, shortly after it throws SIGSEGV error.

Code:

struct Word
{
char* source;
};

int currentCount = 0;
struct Word *currentWord = NULL;

void update(int count, struct Word *word) { //probably called from a different thread
currentCount = count;
currentWord = word;
SDL_Log(“Updated: currentWord->source=%s, currentCount=%i”, currentWord->source, currentCount); //verified that everything was set correctly
SDL_Delay(5000); //!!!Added this
}

int main(int argc, char *argv[]) {
while(true) {
SDL_Delay(1000);
if(currentCount != 0) {
SDL_Log(“currentCount=%i”, currentCount); //outputs what expected
}
if(currentWord != NULL) {
SDL_Log(“currentWord=%s”, currentWord->source); // Prints for about 5 seconds, throws error afterwards: Fatal signal 11 (SIGSEGV) at 0xffffffff (code=1), thread 1162 (SDLThread)
}
}
}

I’m going to dive into general C and SDL multithreading topics to figure out why this happens.

Don’t forget to check .source string is null terminated, a pointer by the
way, could be NULL.