clear dlerror() to prevent old errors bubbling up
authorBill Erickson <berick@esilibrary.com>
Thu, 17 Nov 2011 02:55:59 +0000 (21:55 -0500)
committerBill Erickson <berick@esilibrary.com>
Sun, 20 Nov 2011 23:14:39 +0000 (18:14 -0500)
dlerror() reports and clears errors.  On some systems, it's necessary to
call dlerror() before any calls to dlopen() and dlsysm() so that the
error state can be reset before future calls to dlerror().  Otherwise,
dlerror() may report errors from previous system calls even after a call
to dlopen() or dlsysm().

Problem was originally seen on FreeBSD.

! Unable to locate method symbol [osrfAppInitialize] for app
 opensrf.dbmath: Undefined symbol "_nss_cache_cycle_prevention_function"

See also:

http://stackoverflow.com/questions/3559147/dlerror-undefined-symbol-nss-cache-cycle-prevention-function-on-freebsd-7-2

Signed-off-by: Bill Erickson <berick@esilibrary.com>
src/libopensrf/osrf_application.c

index 57a372b..9a6040e 100644 (file)
@@ -149,6 +149,8 @@ int osrfAppRegisterApplication( const char* appName, const char* soFile ) {
 
        osrfLogInfo( OSRF_LOG_MARK, "Registering application %s with file %s", appName, soFile );
 
+    dlerror(); // clear any existing errors
+
        // Open the shared object.
        void* handle = dlopen( soFile, RTLD_NOW );
        if( ! handle ) {
@@ -167,6 +169,8 @@ int osrfAppRegisterApplication( const char* appName, const char* soFile ) {
        // Add the newly-constructed app to the list.
        osrfHashSet( _osrfAppHash, app, appName );
 
+    dlerror(); // clear any existing errors
+
        // Try to run the initialize method.  Typically it will register one or more
        // methods of the application.
        int (*init) (void);
@@ -211,6 +215,8 @@ static void osrfAppSetOnExit(osrfApplication* app, const char* appName) {
        /* see if we can run the initialize method */
        char* error;
        void (*onExit) (void);
+    dlerror(); // clear any existing errors
+
        *(void **) (&onExit) = dlsym(app->handle, "osrfAppChildExit");
 
        if( (error = dlerror()) != NULL ) {
@@ -238,6 +244,7 @@ int osrfAppRunChildInit(const char* appname) {
        char* error;
        int ret;
        int (*childInit) (void);
+    dlerror(); // clear any existing errors
 
        *(void**) (&childInit) = dlsym(app->handle, "osrfAppChildInit");
 
@@ -597,6 +604,7 @@ int osrfAppRunMethod( const char* appName, const char* methodName,
 
                // Function pointer through which we will call the function dynamically
                int (*meth) (osrfMethodContext*);
+        dlerror(); // clear any existing errors
 
                // Open the function that implements the method
                meth = dlsym(app->handle, method->symbol);