Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

APP crashes easily #824

Closed
hui0613 opened this issue Jul 8, 2023 · 1 comment
Closed

APP crashes easily #824

hui0613 opened this issue Jul 8, 2023 · 1 comment
Labels

Comments

@hui0613
Copy link

hui0613 commented Jul 8, 2023

Versions

  • Python : 3.10.10
  • MacOS version : 13.4.1
  • XCode Version : 14.2
  • Cython version : 0.29.35

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

No tasks being tracked yet.
@hui0613 hui0613 changed the title AP APP crashes easily Jul 8, 2023
@github-actions
Copy link

👋 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants