Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android - native global object destructor never called

I load a shared library, with the classic System.loadLibrary() method, from a static java block. The library has global object allocated statically:

class Foo
{
public:
   Foo()
   {
   }

   ~Foo()
   {
      logtofile( "Foo::~Foo() called" );
   }
}


Foo dummy;

The global function logtofile writes a log file on the sdcard. While the constructor gets called when the library is loaded, it seems to me that the destructor is never called. I expected that between two constructors calls there should be a destructor call. So the question is: when the Foo destructor gets called?

like image 948
Marco Masci Avatar asked Jun 14 '26 20:06

Marco Masci


1 Answers

Android never unloads native libraries; usually the app process dies spontaneously when the system decides that it needs its resources - and it does an equivalent of kill -9, never calling destructors or Java finalize().

like image 70
Alex Cohn Avatar answered Jun 16 '26 09:06

Alex Cohn