The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Android SDL_RWFromFile ERROR
Alexandr


Joined: 08 Feb 2012
Posts: 4
Hi,

This code
Code:
   char *fn = "test.txt";
   SDL_Log ("Try open file: %s", fn);
   for (int i=0; i<3000; ++i) {
      SDL_RWops* rwo = SDL_RWFromFile (fn, "r");
      if (rwo) {
         SDL_Log ("%d Ok", i);
         SDL_RWclose (rwo);
      } else {
         SDL_Log ("ERROR: %s", SDL_GetError());
      }
   }

works like this:
Code:
W/SDL/APP (22744): Try open file: test.txt
W/SDL/APP (22744): 0 Ok
W/SDL/APP (22744): 1 Ok
W/SDL/APP (22744): 2 Ok
.
.
.

W/SDL/APP (22744): 1005 Ok
W/SDL/APP (22744): 1006 Ok
W/SDL/APP (22744): ERROR: Failed to allocate enough JVM local references
W/SDL/APP (22744): ERROR: Failed to allocate enough JVM local references
W/SDL/APP (22744): ERROR: Failed to allocate enough JVM local references
.
.
.

The number of successful attempts to vary, but always limited. Why?
I think this is a SDL error.
gabomdq


Joined: 28 Jul 2011
Posts: 495
Location: Argentina
I think the native code needs to return to the "Java side" to actually release the references, that's why you eventually run out. See here http://groups.google.com/group/android-ndk/browse_thread/thread/490e1ba73e3102b5
gabomdq


Joined: 28 Jul 2011
Posts: 495
Location: Argentina
Also, now that I think about it, I seem to recall that this issue was solved...are you using the latest SDL2 from HG?
Alexandr


Joined: 08 Feb 2012
Posts: 4
gabomdq wrote:
I think the native code needs to return to the "Java side" to actually release the references, that's why you eventually run out. See here http://groups.google.com/group/android-ndk/browse_thread/thread/490e1ba73e3102b5

Thank you. I've already read it, but an error in the SDL can not fix yourself. While using a different way to work with the APK file, without the SDL.

gabomdq wrote:
are you using the latest SDL2 from HG?

The latest version of SDL behaves the same way.
Android
Sam Lantinga


Joined: 10 Sep 2009
Posts: 1765
The Android file code is in here:src/core/android/SDL_android.cpp


From inspection, it looks to me like it's doing the right thing.  Does the Java VM garbage collection need to kick in to actually close the file references?

On Thu, Feb 9, 2012 at 9:52 AM, Alexandr wrote:
Quote:
Hi,

This code



Code:

   char *fn = "recovery.rc";
   SDL_Log ("Try open file: %s", fn);
   for (int i=0; i<3000; ++i) {
      SDL_RWops* rwo = SDL_RWFromFile (fn, "r");
      if (rwo) {
         SDL_Log ("%d Ok", i);
         SDL_RWclose (rwo);
      } else {
         SDL_Log ("ERROR: %s", SDL_GetError());
      }
   }
   return 0;




works like this:



Code:

W/SDL/APP (22744): Try open file: recovery.rc
W/SDL/APP (22744): 0 Ok
W/SDL/APP (22744): 1 Ok
W/SDL/APP (22744): 2 Ok
.
.
.

W/SDL/APP (22744): 1005 Ok
W/SDL/APP (22744): 1006 Ok
W/SDL/APP (22744): ERROR: Failed to allocate enough JVM local references
W/SDL/APP (22744): ERROR: Failed to allocate enough JVM local references
W/SDL/APP (22744): ERROR: Failed to allocate enough JVM local references
.
.
.




The number of successful attempts to vary, but always limited. Why?
I think this is a SDL error.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Android
Paulo Pinto
Guest

As long as the code is calling close() it should suffice.

On Sat, Feb 11, 2012 at 7:36 AM, Sam Lantinga wrote:
Quote:
The Android file code is in here:src/core/android/SDL_android.cpp


From inspection, it looks to me like it's doing the right thing.  Does the Java VM garbage collection need to kick in to actually close the file references?

On Thu, Feb 9, 2012 at 9:52 AM, Alexandr wrote:


Quote:
Hi,

This code



Code:

   char *fn = "recovery.rc";
   SDL_Log ("Try open file: %s", fn);
   for (int i=0; i<3000; ++i) {
      SDL_RWops* rwo = SDL_RWFromFile (fn, "r");
      if (rwo) {
         SDL_Log ("%d Ok", i);
         SDL_RWclose (rwo);
      } else {
         SDL_Log ("ERROR: %s", SDL_GetError());
      }
   }
   return 0;




works like this:



Code:

W/SDL/APP (22744): Try open file: recovery.rc
W/SDL/APP (22744): 0 Ok
W/SDL/APP (22744): 1 Ok
W/SDL/APP (22744): 2 Ok
.
.
.

W/SDL/APP (22744): 1005 Ok
W/SDL/APP (22744): 1006 Ok
W/SDL/APP (22744): ERROR: Failed to allocate enough JVM local references
W/SDL/APP (22744): ERROR: Failed to allocate enough JVM local references
W/SDL/APP (22744): ERROR: Failed to allocate enough JVM local references
.
.
.




The number of successful attempts to vary, but always limited. Why?
I think this is a SDL error.




_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org





_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

gabomdq


Joined: 28 Jul 2011
Posts: 495
Location: Argentina
There's a mention to a related bug in that barren land that is the Android bug tracker. http://code.google.com/p/android/issues/detail?id=2123
It was closed in May 2009, but there's a comment stating that the issue was present as of March 2010...perhaps it is still there, or perhaps you are using an old NDK/Android version?
gabomdq


Joined: 28 Jul 2011
Posts: 495
Location: Argentina
I submitted a fix for this issue here: http://bugzilla.libsdl.org/show_bug.cgi?id=1417 Can you try it out?
Alexandr


Joined: 08 Feb 2012
Posts: 4
Sam Lantinga wrote:
Does the Java VM garbage collection need to kick in to actually close the file references?

Yes, kick it would be nice.

gabomdq wrote:
It was closed in May 2009, but there's a comment stating that the issue was present as of March 2010...perhaps it is still there, or perhaps you are using an old NDK/Android version?

r7

gabomdq wrote:
I submitted a fix for this issue here: http://bugzilla.libsdl.org/show_bug.cgi?id=1417 Can you try it out?

I picked up a copy of the SDL library a couple of days ago, so:
Code:
hg clone http://hg.libsdl.org/SDL


In my file SDL_android.cpp has the following lines:
Code:

    .
    .
    .
    bool allocatedLocalFrame = false;

    if (mEnv->PushLocalFrame(16) < 0) {
        SDL_SetError("Failed to allocate enough JVM local references");
        goto failure;
    } else {
        allocatedLocalFrame = true;
    }
    .
    .
    .
    if (allocatedLocalFrame) {
        mEnv->PopLocalFrame(NULL);
    }
    .

it is a bit like a place where you applied the patch
Android SDL_RWFromFile ERROR
gabomdq


Joined: 28 Jul 2011
Posts: 495
Location: Argentina
Quote:
I submitted a fix for this issue here: http://bugzilla.libsdl.org/show_bug.cgi?id=1417 Can you try it out?



I picked up a copy of the SDL library a couple of days ago, so:


Code:



hg clone http://hg.libsdl.org/SDL




In my file SDL_android.cpp has the following lines:


Code:




    .
    .
    .
    bool allocatedLocalFrame = false;

    if (mEnv->PushLocalFrame(16) < 0) {
        SDL_SetError("Failed to allocate enough JVM local references");
        goto failure;
    } else {
        allocatedLocalFrame = true;
    }
    .
    .
    .
    if (allocatedLocalFrame) {
        mEnv->PopLocalFrame(NULL);
    }
    .



it is a bit like a place where you applied the patch

_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

This fixes the file closing function, you are probably looking at the file open function. I believe the patch should apply cleanly.
Re: Android SDL_RWFromFile ERROR
Alexandr


Joined: 08 Feb 2012
Posts: 4
gabomdq wrote:
This fixes the file closing function, you are probably looking at the file open function. I believe the patch should apply cleanly.

sorry, hurry)

Yes, your patch has decided my issue. Thank you very much.

Last question: what do these lines in a function of Android_JNI_FileClose ?
Android SDL_RWFromFile ERROR
gabomdq


Joined: 28 Jul 2011
Posts: 495
Location: Argentina
2012/2/11 Alexandr
Quote:



gabomdq wrote:

This fixes the file closing function, you are probably looking at the file open function. I believe the patch should apply cleanly.



sorry, hurry)

Yes, your patch has decided my issue. Thank you very much.

Last question: what do these lines in a function of Android_JNI_FileClose ?


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



There's a limit of "local references" for the native code of 512 entries. The FileClose function was leaking 1 reference per call, and FileOpen tries to reserve 16 references when it begins, so when you open the 497th file, it fails (as there's only 15 spaces left in the table). If you want to learn more search for Android JNI Local and Global references, there's quite a bit of docs online.

--
Gabriel.
Android SDL_RWFromFile ERROR
Sam Lantinga


Joined: 10 Sep 2009
Posts: 1765
Thanks to Gabriel and Tim's efforts, this bug is fixed:http://hg.libsdl.org/SDL/rev/1893d507ba42


Cheers!

On Sat, Feb 11, 2012 at 5:27 PM, Gabriel Jacobo wrote:
Quote:
2012/2/11 Alexandr
Quote:



gabomdq wrote:

This fixes the file closing function, you are probably looking at the file open function. I believe the patch should apply cleanly.



sorry, hurry)

Yes, your patch has decided my issue. Thank you very much.

Last question: what do these lines in a function of Android_JNI_FileClose ?




_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org




There's a limit of "local references" for the native code of 512 entries. The FileClose function was leaking 1 reference per call, and FileOpen tries to reserve 16 references when it begins, so when you open the 497th file, it fails (as there's only 15 spaces left in the table). If you want to learn more search for Android JNI Local and Global references, there's quite a bit of docs online.

--
Gabriel.


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org