Problem
My application that uses dlopen() and dlclose() keeps using more and more memory allowing it to grow in size indefinitely.
Cause
Dlopen(S) uses an allocation scheme which does not free memory that is allocated for its use. Dlopen() calls _rtmalloc(), which has no corresponding _rtfree() function for dlclose() to call. In fact, there is no _rtfree() in the OSR5 rtld.
This problem is not likely to manifest itself unless an application is opening and closing a large number of libraries.
Solution
This has been reported to SCO Engineering and is scheduled to be addressed in an upcoming release.
The following workarounds have been found:1. Modify the program to not call dlclose on OpenServer. An example is provided here to show this modification.
Taking the following sample program:#include <stdio.h>
#include <dlfcn.h>void main()
{
void *h;
int i = 0;
char s[1000];for ( ; ; i++) {
h = dlopen("./myso.so", RTLD_LAZY);
if (h == NULL)
printf("Failed to load shared object\n");
else
dlclose(h); printf("%d ", i);
}
}With the modification, it becomes:#include <stdio.h>
#include <dlfcn.h>void main()
{
void *h;
int i = 0;
char s[1000]; for ( ; ; i++) {
h = dlopen("./myso.so", RTLD_LAZY);
if (h == NULL)
printf("Failed to load shared object\n");
#ifdef SCO
else
dlclose(h);
#endif printf("%d ", i);
}
}2. Compile your program with a separate object file compiled from this source:
dlclose(){}This is a stub function which makes "dlclose()" do
nothing. It is permissible to open the same shared object
multiple times. In our dynamic library code, there is no harm
in doing so. The leak comes about only when libraries are closed
and then reopened. There is also no symbol referencing problem;
even if multiple libraries are opened which contain the same
symbol names. A symbol lookup always searches them in most-
recently-opened order -- the "newest" version of the symbol
is always obtained.This workaround may not be suitable in the following cases:
a. If the application depends on getting dlsym() failures
after a shared object has been "closed".b. If subsequent dlopen() calls are being made on the same file name, but the contents have changed - the application will dump core.
If subsequent calls are being made on different names --
dlopen("mylib.2001-02-14.12:00.so");
dlclose();
dlopen("mylib.2001-02-14.12:30.so");
dlclose();
...-- then it will neither help nor hurt.
3. Use a macro in one of the header files, along the lines of:
#include <dlfcn.h>
...
#if defined(OpenServer) && (OSREL < 507)
#define dlclose(ignored) 0 // workaround dlclose() memory leak
#endif /* OpenServer < 507 */4. Redesign the application so that instead of calling dlopen/dlclose
every time, a signal is sent to the application when the
application libraries are updated. Then the application would
need to do the dlopen/dlclose only when the signal is received.5. Recompile the application using the UDK, which has the problem to a much lesser degree. The UDK on OSR5 uses the OSRcompat libraries including /udk/usr/lib/libc.so.1, which provides the dl*() routines, implemented the same way as on UnixWare 7.
6. Re-write the application so that it loads and unloads the shared object once, rather than opening and closing many small dynamically linked libraries (DLL's). This can be done by combining them into one DLL.
7. Use statically linked libraries instead of using DLLs
NOTE: Some of the above workarounds cannot handle the case where the shared object (DLL) gets updated. One such case would be when, after modifying a shared object, it is necessary to call dlclose() to un-busy the shared object file and it is then necessary to call dlopen() on the new file in order to have access to the changes. In these cases, the application may have to be stopped and restarted.