Boost logo

Boost :

From: Robert Dailey (rcdailey_at_[hidden])
Date: 2008-07-08 19:12:45


On Tue, Jul 8, 2008 at 5:34 PM, Jeremy Pack <rostovpack_at_[hidden]> wrote:
> Robert,
>
> The first answer in my e-mail applies then - you can use the same
> factory_map for as many shared libraries as you'd like.
>
> Other answers below:
>
>
>> Thanks for responding Jeremy.
>>
>> I'm a little confused about your response, but perhaps that's because
>> I don't know as much about shared libraries as I should. Basically,
>> I'm assuming that the shared libraries must remain open in order to
>> continue to use the factories.
>
>
> Yes.
>
>
>> I'm not sure what you mean by closing
>> the shared libraries I'm not using.
>
>
> If you have no need to close the shared libraries (ie, you aren't getting
> low on RAM), then don't. You should only need that if you are loading many,
> many shared libraries. By default, they remain open.
>
>
>> I'm technically using the shared
>> libraries every time I call factory::create(). However, once I
>> concatenate the factory_map for each shared library, I do not use the
>> factory_map any longer. I simply keep them stored in a
>> boost::ptr_vector to maintain lifetime, and when I no longer need
>> access to my concatenated list of factories, I simply destroy the
>> ptr_vector of factory_map objects.
>
>
>
> 1 - The factory object is copyable - so you can just copy all of the
> std::map objects returned by factory_map::get into another map. You can then
> safely destroy the factory_map.
> 2 - You can just use a single factory_map anyway.
>
>
> Say, for example, I load 3 shared libraries. As I add more shared
>> libraries, I am capable of creating more types, since each shared
>> library provides a way for me to extend the capabilities of my factory
>> (lets it create more types). In the case of having 3 shared libraries,
>> the m_factories member would simply be 3 in size. I do not use the
>
> m_factories member anywhere, it simply serves the purpose of
>> maintaining the lifetime of the shared libraries.
>
>
> You don't need to do that to maintain the lifetime of the shared libraries.
> factory_map does not open or close shared libraries. Only the shared_library
> object does that. By default, shared libraries are left open forever (this
> is an option in the shared_library constructor). Feel free to delete
> factory_maps as soon as you have retrieved the factories that you need.
>
> If you could explain in more detail what you mean by using the same
>> shared_library for multiple libraries, I would appreciate it. I
>> figured there was a 1-to-1 mapping between factory_map and DLLs (on
>> windows).
>> ____________________________
>>
>
> Check out the example in
> libs/extension/examples/multiple_inheritance/main_mi.cpp.
>
> It does something like this:
>
> factory_map my_map;
> load_single_library(my_map, "first_library.extension", "extension_export");
> load_single_library(my_map, "second_library.extension", "extension_export");
> load_single_library(my_map, "third_library.extension", "extension_export");
>
> Thus, it uses the same factory_map for every shared library.
>
> If you only want the factories of a certain type (and delete the rest of the
> factory_map), you can then do the following:
>
> factory_map *my_map;
> load_single_library(*my_map, "first_library.extension", "extension_export");
> load_single_library(*my_map, "second_library.extension",
> "extension_export");
> std::map<std::string, factory<computer> > factory_list;
> factory_list.swap(my_map->get<computer, std::string>());
> delete my_map;
>
> I think that in your case, this is what you want to do. Does that make more
> sense?

Yes, your explanation helps out tremendously. However, I noticed that
load_single_library() simply deletes (by allowing it to fall out of
scope) the shared_library object it creates as the function returns.
Does this mean that it does not close the library when it is
destroyed? So I will need to be responsible for calling
shared_library::close() to release the DLL when I am done using it?

I noticed also (you mentioned this) that there's an auto_close
construction parameter in shared_library, however this value does not
seem to get used anywhere. In fact, shared_library does not have a
destructor that I can see. Does automatic unloading of shared
libraries not work on destruction of shared_library objects even when
setting auto_close to true?

I'm assuming that by closing a shared_library, any factory objects
created from that shared_library will not function anymore, since it
won't have access to the classes it is creating.

Thanks again for your continued help!

PS: Do you have an online version of the trunk's documentation for
Boost.Extension? I haven't been able to find this. Thank you.


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk