Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / August 2005



Tip: Looking for answers? Try searching our database.

dlclose not calling destructors

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Yuval Kfir - 11 Aug 2005 15:37 GMT
I have a simple program composed of a main executable and a dynamic
library (built with -dynamic -dynamiclib).  The executable is _not_
linked with the library.  It calls dlopen() to open the dynamic lib,
calls dlsym() to obtain the address of a function, calls that function
and then calls dlclose().

The problem is that while dlopen() does load the library and call its
global constructors, dlclose() does NOT call the library's global
destructors.  This can be very clearly seen if I use the environment
variable DYLD_PRINT_INITIALIZERS=1 - it calls the initializer function
of the dynamic lib at the dlopen(), and it calls the termination
function only when it calls exit(), not when it calls dlclose().

Is there any way to make dlclose() behave as it does on other Unix
systems, such as Linux and Solaris?

Thanks for any information,
- Yuval
Eric Albert - 11 Aug 2005 21:45 GMT
> I have a simple program composed of a main executable and a dynamic
> library (built with -dynamic -dynamiclib).  The executable is _not_
[quoted text clipped - 11 lines]
> Is there any way to make dlclose() behave as it does on other Unix
> systems, such as Linux and Solaris?

Are you sure your library is actually being unloaded?  Last I knew,
dlclose didn't unload dylibs on Mac OS X.

If you don't have to link against this library at compile time, I'd
suggest building it as a bundle (-bundle) rather than a dylib
(-dynamiclib).  Bundles should have the .bundle extension and are
unloadable.

-Eric

Signature

Eric Albert         ejalbert@cs.stanford.edu
http://outofcheese.org/

Yuval Kfir - 12 Aug 2005 12:29 GMT
> Are you sure your library is actually being unloaded?  Last I knew,
> dlclose didn't unload dylibs on Mac OS X.
[quoted text clipped - 3 lines]
> (-dynamiclib).  Bundles should have the .bundle extension and are
> unloadable.

No, in fact, I'm sure it is NOT being unloaded. That is exactly the
problem. So despite documentation to the contrary, dlclose() is
essentially a no-op for .dylib libraries on Mac OS/X? Ugh.

I will look into the .bundle solution. Thank you for the information!

- Yuval
Alwyn - 12 Aug 2005 12:32 GMT
> Are you sure your library is actually being unloaded?  Last I knew,
> dlclose didn't unload dylibs on Mac OS X.

Then is the documentation wrong yet again?

> Discussion
> This function decreases the reference count of the image referenced by
[quoted text clipped - 4 lines]
> queried with dlerror.
> See also dlopen, dlerror.

Alwyn
Eric Albert - 12 Aug 2005 18:13 GMT
In article
<dt015a1979-CD8313.12322412082005@text.news.blueyonder.co.uk>,

> > Are you sure your library is actually being unloaded?  Last I knew,
> > dlclose didn't unload dylibs on Mac OS X.
[quoted text clipped - 9 lines]
> > queried with dlerror.
> > See also dlopen, dlerror.

You're looking at the Panther man page, not the Tiger one -- the dl*
APIs were rewritten from scratch for Tiger -- but yeah, I think it's
wrong.  Unfortunately, unloading dylibs is a lot harder than you might
think.

-Eric

Signature

Eric Albert         ejalbert@cs.stanford.edu
http://outofcheese.org/

Alwyn - 12 Aug 2005 18:36 GMT
> You're looking at the Panther man page, not the Tiger one

Dynamic Loader API Reference: Dynamic Loader Compatibility Functions
(Last updated: 2005-06-04)

> -- the dl* APIs were rewritten from scratch for Tiger

So I believe.

> -- but yeah, I think it's
> wrong.  Unfortunately, unloading dylibs is a lot harder than you might
> think.

That may well be, but their documentation consistently fails to reflect
the facts.

Alwyn
Alwyn - 12 Aug 2005 18:17 GMT
> Are you sure your library is actually being unloaded?  Last I knew,
> dlclose didn't unload dylibs on Mac OS X.

You're quite right, it doesn't on Mac OS 10.4.2. This is very
surprising, in the light of the recently updated documentation at:
<http://developer.apple.com/documentation/DeveloperTools/Conceptual/Dynam
icLibraries/index.html#//apple_ref/doc/uid/TP40001869>
> The client must call dlclose when it¹s finished using the dynamically loaded
> library (for example, when the module that opened the library has finished
> its task).
...
> Calls to dlclose decrement the library handle¹s reference count. Therefore,
> you must balance every call to dlopen with a call to dlclose. When the
> reference count for a library handle reaches 0, the dynamic loader may remove
> the library from the address space of the application.

The wording "may remove" above is interesting, however.

Alwyn
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.