When to kill a native thread?

932 views
Skip to first unread message

MatthiasM

unread,
Mar 13, 2011, 12:20:37 PM3/13/11
to android-ndk
Targeting 2.2, I have a problem with a native thread that I start from
within my library (using pthreads). When my main app gets killed, the
NDK part of my code is never informed (AFAIK), so it will never
returns its resources and doesn't have a chance to kill the thread. I
have thought about a watchdog timer, but it seems like a big effort
for a little thing.

What is the common practice for NDK code to release resources in case
the Java code gets killed?

This is the last major issue before I can release my app. So any help
is greatly appreciated.

- Matthias

http://www.matthiasm.com/einstein.html

Dianne Hackborn

unread,
Mar 13, 2011, 2:52:16 PM3/13/11
to andro...@googlegroups.com, MatthiasM
You need to define what you mean by "my main app gets killed."  If it is the normal killing a process in the background, there is nothing for you to do, because the entire process is going away right then and there.  If it is the hosting activity being finished, you can implement Activity.onDestroy() to clean up native resources.


--
You received this message because you are subscribed to the Google Groups "android-ndk" group.
To post to this group, send email to andro...@googlegroups.com.
To unsubscribe from this group, send email to android-ndk...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/android-ndk?hl=en.




--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

MatthiasM

unread,
Mar 13, 2011, 4:20:16 PM3/13/11
to android-ndk

Thanks. Well, if it gets terminated in the background, I do get the
onDestroy() call and all is fine.

But when I am in the "Application Info" panel and click "Force Stop",
or when I am in the task manager and click "End", I don't get a
message whatsoever. In a purely native application, even when
crashing, it would decrement the usage counter of the dynamically
linked library and call the exit code.

Any other idea how I can get a signal when the app is killed?

Dianne Hackborn

unread,
Mar 13, 2011, 5:22:03 PM3/13/11
to andro...@googlegroups.com, MatthiasM
On Sun, Mar 13, 2011 at 1:20 PM, MatthiasM <melcher....@googlemail.com> wrote:
Thanks. Well, if it gets terminated in the background, I do get the
onDestroy() call and all is fine.

onDestroy() is not called when an app is terminated in the background; it is called when an activity is finished.
 
But when I am in the "Application Info" panel and click "Force Stop",
or when I am in the task manager and click "End", I don't get a
message whatsoever. In a purely native application, even when
crashing, it would decrement the usage counter of the dynamically
linked library and call the exit code.
Any other idea how I can get a signal when the app is killed?

You can't.  Force stop kills the app, all of its processes, everything running inside of those processes, and all resources held by those processes.  You can't get in the way of this.

You don't need to decrement a usage counter of a linked library.  The entire processes is being killed.

MatthiasM

unread,
Mar 13, 2011, 6:06:17 PM3/13/11
to android-ndk


On Mar 13, 10:22 pm, Dianne Hackborn <hack...@android.com> wrote:
> On Sun, Mar 13, 2011 at 1:20 PM, MatthiasM
> <melcher.matth...@googlemail.com>wrote:
>
> > Thanks. Well, if it gets terminated in the background, I do get the
> > onDestroy() call and all is fine.
>
> onDestroy() is not called when an app is terminated in the background; it is
> called when an activity is finished.

OK, thanks. It doesn't seem to get called here. OnStop, onResume, and
all the other ones in my app do get called as expected, but I also
don't have a way to finish the main Activity.

> > But when I am in the "Application Info" panel and click "Force Stop",
> > or when I am in the task manager and click "End", I don't get a
> > message whatsoever. In a purely native application, even when
> > crashing, it would decrement the usage counter of the dynamically
> > linked library and call the exit code.
> > Any other idea how I can get a signal when the app is killed?
>
> You can't.  Force stop kills the app, all of its processes, everything
> running inside of those processes, and all resources held by those
> processes.  You can't get in the way of this.

Thanks. I must be doing something else wrong then. I load the dll
using "static { System.loadLibrary("einstein"); }". Then in
"onCreate", I call the library, and before I return, I start a
pthread. I would expect that killing the process would also kill all
child processes - including the native ones - but neither on the
emulator nor on the device this is the case. The thread continues
happily, keeping all the resources that were allocated from within the
native library. And since it does not know that the parent process is
gone, it will eat resources. I have not checked yet if another launch
of the app will create another instance of the dll, but that would be
bad.

> You don't need to decrement a usage counter of a linked library.  The entire
> processes is being killed.

But that does not kill the dll ("shared library" would be the Unix
term, I had that wrong earlier). I was expecting that at least "void
JNI_OnUnload(JavaVM* vm, void* reserved);" gets called when the parent
process is killed.

I will first try to create a Java thread and use that to kick of the
native thread and see if that fixes things.

If not, I will probably have to add some dead-man timer.

Thanks,

Matthias

MatthiasM

unread,
Mar 13, 2011, 6:19:17 PM3/13/11
to android-ndk
NEVER MIND. I AM AN IDIOT.

I don't know why, but I still had a process running that would output
log messages. I have no idea how I did that, but it all vanished after
I rebooted the device and reinstalled the emulator.

I destilled everything down to a minimal example, and the thread
actually vanishes as expected. I then tried the big app and it does it
now too.

I apologize for the noise and thank you for the very quick help!

- Matthias
Reply all
Reply to author
Forward
0 new messages