kivy / kivy-ios

Toolchain for compiling Python / Kivy / other libraries for iOS
https://kivy.org/docs/guide/packaging-ios.html
MIT License
758 stars 238 forks source link

APP crashes easily #824

Closed hui0613 closed 8 months ago

hui0613 commented 12 months ago

Versions

I am kivy-ios and built libpython3.so and other dependent .so libraries. Using these libraries and swift to develop a static library, when initializing the python interpreter, it is easy to cause the app to crash. The following is a crash call stack。

image image

Below is my code for initialization. Almost all crashes happen in Py_Initialize(); method, I can't figure out what's going on here

void python_main(NSString * resourcePath){
    chdir("YourApp");

//    printf("NSBundle bundleForClass:self.class %s\n",resourcePath);
    NSString *python_home = [NSString stringWithFormat:@"PYTHONHOME=%@", resourcePath, nil];
    printf("python_home\n %s \n", (char *)[python_home UTF8String]);
    putenv((char *)[python_home UTF8String]);

    NSString *python_path = [NSString stringWithFormat:@"PYTHONPATH=%@:%@/lib/python3.9/:%@/lib/python3.9/site-packages:.", resourcePath, resourcePath, resourcePath, nil];
//    printf("python_path\n %s\n", (char *)[python_path UTF8String]);
    putenv((char *)[python_path UTF8String]);

    NSString *tmp_path = [NSString stringWithFormat:@"TMP=%@/tmp", resourcePath, nil];
//    printf("tmp_path\n %s\n", (char *)[tmp_path UTF8String]);
    putenv((char *)[tmp_path UTF8String]);

    NSString *document_path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *log_path = [NSString stringWithFormat:@"WEARSERVICE_LOG_PATH=%@/persimwear_log/wearcore", document_path, nil];
//    printf("log_path= %s\n", (char *)[log_path UTF8String]);
    putenv((char *)[log_path UTF8String]);

    printf("-------");
    [BridgeUtils writeStrToFile:@"Initializing python"];

    Py_Initialize();

    // If other modules are using the thread, we need to initialize them before.
    PyEval_InitThreads();
#define MAIN_EXT @"pyc"
    int ret = 0;
//    const char * prog = [
//        [resourcePath stringByAppendingString:@"/bootstrap.pyc"] cStringUsingEncoding:
//        NSUTF8StringEncoding];

    NSLog(@"Running main.py: %s", "use import bootstrap");
    @try {
        PyRun_SimpleString("import bootstrap");
    } @catch (NSException *exception) {
        sleep(500);
        PyRun_SimpleString("import bootstrap");
    }
    main_running = true;
    PyEval_ReleaseLock();
    while (main_running && !main_destroy) {
        sleep(1);
    }

    Py_Finalize();
    NSLog(@"Leaving");
}

+(void) python_start:(NSString * ) resourcePath{

    // resourcePath is a bundle file path, which stores all python source code
    if(!main_running){
        [BridgeUtils writeStrToFile:@"python_start"];
        NSLog(resourcePath);
        pthread_attr_t thread_attr;
        pthread_attr_init(&thread_attr);
        pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
//        NSString * resourcePath = [[NSBundle bundleForClass:self.class] resourcePath];
        pthread_create(&py_main_thread, &thread_attr, (void *) python_main, CFBridgingRetain(resourcePath));
        pthread_attr_destroy(&thread_attr);
        int retry = 10;
        while(!main_running && retry--){
            sleep(1);
        }
    }
}
### Tasks
github-actions[bot] commented 8 months ago

👋 We use the issue tracker exclusively for bug reports and feature requests. However, this issue appears to be a support request. Please use our support channels to get help with the project.

If you're having trouble installing or using kivy-ios, maybe you could be interested to installation and requirements.

Let us know if this comment was made in error, and we'll be happy to reopen the issue.