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 / June 2006



Tip: Looking for answers? Try searching our database.

Loading 2 libraries, getting 1 handle

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
shai@waves.com - 25 Jun 2006 08:00 GMT
I have the following situation:

I compile and link a Dynamic library and save it to disk as a.dylib.
I now duplicate a.dylib and call the duplicated file b.dylib.

I now run an application that loads a.dylib by calling dlopen. The
application also loads b.dylib by calling dlopen.

To my surprise both calls to dlopen return the same handle. The globals
for a.dylib are initialized, the globals for b.dylib are not.

Somehow dlopen "knows" that a.dylib and b.dylib are the same library.

What gives?

[I'm using Xcode 2.3 / gcc 4.0.1]

Shai Shasag
Reinder Verlinde - 25 Jun 2006 22:35 GMT
> I have the following situation:
>
[quoted text clipped - 10 lines]
>
> What gives?

I have never really programmed dynamic libraries on Mac OS X, but I
would _guess_ that the cause of this is that the Dynamic linker does not
use the file names to check whether a given library already is loaded:

<http://developer.apple.com/documentation/DeveloperTools/Conceptual/Dynam
icLibraries/index.html> states:

  "The static linker records the filenames of each of the dependent
   libraries at the time the application is linked. This filename is
   known as the dynamic library¹s install name. The dynamic loader uses
   the application¹s dependent libraries¹ install names to locate them
   in the file system"

<http://developer.apple.com/documentation/DeveloperTools/Reference/MachOR
eference/Reference/reference.html#//apple_ref/c/func/dlopen> states
about dlopen:

  "This function examines the Mach-O file specified by path. If the
   image is compatible with the current process and has not already
   been loaded into the process, the image is loaded and linked. If the
   image contains initializer functions, they are executed before this
   function returns."

Note the phrase "If the _image_ [...] has not already been loaded into
the process". I would guess this is _not_ equivalent to: "If the _file_
[...] has not already been loaded into the process".

I guess this is done to make the system load a library only once, even
if multiple copies are opened, or if it is opened using multiple paths.
That way, applications can ship with their own copy of the library
without incurring memory overhead. There still is disk overhead, but
disk is so cheap nowadays that disk overhead is not considered a real
issue.

Reinder
shai@waves.com - 27 Jun 2006 07:00 GMT
> Note the phrase "If the _image_ [...] has not already been loaded into
> the process". I would guess this is _not_ equivalent to: "If the _file_
> [...] has not already been loaded into the process".

Thanks Reinder, This is also the conclusion I reached. Apparently the
DLL have an "inner name" that uniquely identifies them. This name can
be changed with install_name_tool.

Thanks again,

Shai
 
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



©2008 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.