Need help with what I think could be a ctrulib bug

Discussion in '3DS - Homebrew Development and Emulators' started by nop90, Sep 16, 2016.

  1. nop90
    OP

    nop90 GBAtemp Maniac

    Member
    1,344
    2,030
    Jan 11, 2014
    Italy
    Rome
    I found a strange bug in all my homebrew in CIA format.

    While in the homebrew, if I pres the home button the system opens the home menu, from here if I close the 3ds case and then open it again, both screens remain black and the system seems to be frozen.

    It doesn't seems to be a problem in my code, but before opening an isuue on the ctrulib github I ask the devs of the forum to check it.

    The problem happens with the following simple code too:

    Code:
    #include <stdio.h>
    
    #include <3ds.h>
    
    //----------------------------------------------------------------------------
    int main(int argc, char **argv) {
    //----------------------------------------------------------------------------
    
       PrintConsole topScreen;
    
       gfxInitDefault();
    
       consoleInit(GFX_TOP, &topScreen);
    
       consoleSelect(&topScreen);
    
       printf("Press START to exit\n");
    
       int i=0;
       while(aptMainLoop()) {
    
         gfxSwapBuffers();
         gfxFlushBuffers();
         gspWaitForVBlank();
    
         hidScanInput();
         u32 kDown = hidKeysDown();
    
         if (kDown & KEY_START)
           break; // break in order to return to hbmenu
    
       printf("%i\n",i++);
    
       }
    
       gfxExit();
       return 0;
    }
    
    You can find attached the rsf file I used for the test (UniqueId 0xffff that I use to test pre release cias)

    Maybe it isn't a ctrulib problem but only of my CFW version (it's RXTool, I know it's outdated, but I use it only to test my hoembrew in CIA format)

    If you have time please test this simple homebrew and tell me if the problem happens with your CFW and ctrulib version.

    Thanks in advance. NOP90
     

    Attached Files:

  2. MarcusD

    MarcusD nobody's hero

    Member
    1,141
    2,200
    Oct 16, 2015
    Hungary
    a pixel between life and death
    I can confirm this is an issue! And I think I know why!

    You see, ctrulib only processes APT events in aptMainLoop(), but if you are in the home menu, iirc the main thread gets suspended.
    In official applications, APT events are handled by a separate thread which is NOT suspended when you suspend the application, meaning if you close the lid, and open it back, it's still handled by the application.

    Also, this is both the 3DS's fault, and ctrulib's fault :P
     
  3. nop90
    OP

    nop90 GBAtemp Maniac

    Member
    1,344
    2,030
    Jan 11, 2014
    Italy
    Rome
    So to solve ithis we could create in the app a separate thread that hadles the exiting from the sleep mode, and inserting a sleep() call in the aptmainloop() function to make run the second thread.

    What about it?
     
  4. MarcusD

    MarcusD nobody's hero

    Member
    1,141
    2,200
    Oct 16, 2015
    Hungary
    a pixel between life and death
    Well, ye, we could replicate how official apps do it, but if your app crashes, and you try to return to *hax, then it'll still run in the background, causing many troubles, so it doesn't sound like a good idea :/

    I mean, technically we can detect if we're running as a .cia, so it should be possible to make or not make a thread for APT, depending on how you run the program.
     
  5. nop90
    OP

    nop90 GBAtemp Maniac

    Member
    1,344
    2,030
    Jan 11, 2014
    Italy
    Rome
    After opening an issue on ctrulib github, Fincs informed me that this problem has been solved in ctrulib at the end of july, so updating ctrulib solves the problem.

    I tested it and can confirm that now everything works fine.
     
    hippy dave likes this.