Files
android_development/ndk/tests/dlclose-destruction/jni/main.c
David 'Digit' Turner d155de2400 Update API level 9 sysroot to allow static C++ destructors to be called on dlclose().
This introduces crtbegin_so.o and crtend_so.o and also requires a corresponding
change in the NDK build scripts to use them.

Also add a small test under 'tests/dlclose-destruction' to check that.
Note that this is not a sample (i.e. Android application) per se.

Change-Id: Icf25836363a3ed59310e579ce990aeca868e70e4
2010-06-25 16:17:58 -07:00

49 lines
1.0 KiB
C

#include <stdio.h>
#include <dlfcn.h>
typedef void (*test_func_t)(int *px);
int x;
int main(void)
{
void* lib = dlopen("libtest1.so", RTLD_NOW);
test_func_t test_func;
if (lib == NULL) {
fprintf(stderr, "Can't load library: %s\n", dlerror());
return 1;
}
printf("Loaded !\n");
test_func = dlsym(lib, "test1_set");
if (test_func == NULL) {
fprintf(stderr, "Can't find test function\n");
return 2;
}
x = 0;
test_func(&x);
if (x == 1) {
printf("Test function called !\n");
} else {
fprintf(stderr, "Test function failed to set variable !\n");
return 3;
}
dlclose(lib);
printf("Unloaded !\n");
if (x == 2) {
printf("Test destructor called !\n");
} else if (x == 1) {
fprintf(stderr, "Test destructor was *not* called !\n");
return 4;
} else {
fprintf(stderr, "Test destructor called but returned invalid value (%d)\n", x);
return 5;
}
return 0;
}