简体   繁体   中英

iOS app crashes on app termination with EXC_BAD_ACCESS on global/static variables

In my iOS application, I'm getting an EXC_BAD_ACCESS crash on one of the global variables I use in my C/C++ code when the app is terminated. Please note that the app is not put to background but is terminated on closing the app by specifying to not run in background in the info plist file.

Following is the related code..

#include "chromium/threading/thread_local.h"
#include "libev/ev.c"

namespace simplegetter {


ThreadLocalPointer<ev_io> threadLocal_sock_watcher;
ThreadLocalPointer<ev_timer> threadLocal_timer;


void start_get (my_get_struct* get_struct) {

    ev_io* sock = new ev_io;
    ev_timer* temp_timer = new ev_timer;

    threadLocal_sock_watcher.Set(sock);
    threadLocal_timer.Set(temp_timer);

    //... other code.. 

}

void fire_again(my_get_struct* get_struct, uint32_t rtt) {
    double repeat = get_client_repeat_value(rtt);
    if (repeat < 0.5) repeat = 0.5;
    threadLocal_timer.Get()->repeat = repeat;
    get_struct->prev_timeout_finish_time = CURRENT_TIME_MS;
    ev_timer_again( threadLocalLoop.Get(), threadLocal_timer.Get());

    //.. other code
}

//... other code 

};

threadLocal_timer is the global variable I'm talking about. the start_get method is called on a new thread to fetch a network resource. The fire_again method gets called multiple times on the thread that called start_get method until the operation is completed.

Now the issue is, on app termination (ie when iOS sends an exit() call on the main thread.. (which is because the app is not supposed to stay in suspended state or run in background), iOS seem to be releasing off the global variable ie threadLocal_timer in this case which leads to a crash EXC_BAD_ACCESS in my code at line:

threadLocal_timer.Get()->repeat = repeat;

So, the global variable is released by the iOS before the thread is stopped on termination of the app. My question is, is there any solution on how I can deal with this crash apart from not using static or global variables at all in the above code?

Your question is too vague.

I have written apps that have long-running background processes that depend on global resources, much like you describe.

What I did was to have a global background task count. Each time I started one of these long-running tasks, I called a function on the main thread that incremented the task count.

At the very end of the shutdown code for the long-running tasks, I call another function (on the main thread) that decrements the counter.)

In the code that decrements the thread count, if the count hits zero, I deallocate and nil out my global resources.

With that logic, the global resources are not freed until the last background thread is done with them.

An approach like that would probably work for your problem but I can't be more specific without knowing more about it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM