473,669 Members | 2,526 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

explicitly loaded dlls, virtual functions and templates :(

Hello, All!

One of our target platforms has only 32 MB of virtual memory (Windows CE),
so we decided to explicitly load some of our dlls.

But the classes created inside such dlls are created with vfptr pointed to
address space of the dll. So when DLL is unloaded vtable is destroyed.

The virtual clone() function of such instance creates the class with the
same vfptr :)

The problem could be solved by moving inline functions to cpp files. But the
class is a template :(

Best regards, Vyacheslav Lanovets
Nov 17 '05 #1
7 1423
Maybe I am misunderstandin g your point. But if you want to execute code,
this code must be loaded. Instantiating a class and then unloading the DLL
that implements the class must fail obviously.

"Vyacheslav Lanovets" <xentrax_umail. ru> wrote in message
news:e%******** ********@TK2MSF TNGP15.phx.gbl. ..
Hello, All!

One of our target platforms has only 32 MB of virtual memory (Windows CE),
so we decided to explicitly load some of our dlls.

But the classes created inside such dlls are created with vfptr pointed to
address space of the dll. So when DLL is unloaded vtable is destroyed.

The virtual clone() function of such instance creates the class with the
same vfptr :)

The problem could be solved by moving inline functions to cpp files. But
the class is a template :(

Best regards, Vyacheslav Lanovets

Nov 17 '05 #2
Hello, Marcus!
You wrote on Wed, 10 Aug 2005 22:09:46 +0200:

MH> Maybe I am misunderstandin g your point. But if you want to execute
MH> code, this code must be loaded. Instantiating a class and then
MH> unloading the DLL that implements the class must fail obviously.

Yes, but it's such a pity that it's so hard to mix templates, virtual
functions and explicit loading :(

We had to derive from these templates and put constructos into a dll which
is not unloaded.

Best regards, Vyacheslav Lanovets
Nov 17 '05 #3
I assume I am still missing something here:

<Vyacheslav>
The problem could be solved by moving inline functions to cpp files. But the
class is a template :(
</Vyacheslav>

Why would the problem be solved by moving inline functions to cpp files?
Nov 17 '05 #4
Hello, Marcus!
You wrote on Thu, 11 Aug 2005 17:44:33 +0200:

MH> I assume I am still missing something here:

MH> <Vyacheslav>
MH> The problem could be solved by moving inline functions to cpp files.
MH> But the class is a template :(
MH> </Vyacheslav>

MH> Why would the problem be solved by moving inline functions to cpp
MH> files?

Because classes with inline constructor (or class templates) have
_different_ vtables when created in different DLLs.

When the class template with virtual function clone is created by code in
module A, it's vtable is in module A address space because its
constructor is "inline".
So, when it's constructed in module B, vtable is in module B address space.

The problem is that even if I call ->clone() from module B, I still get a
copy with vtable residing in module A, so I can not use Prototype design
pattern if I want to unload module A.

But as soon as I define constructor in x.cpp, the vtable is placed in module
with x.pp, not in module A or B.

The problem is that this limits usability of templates: in my case I had to
derive all required "instantiations " from class template and declare
constructors of derived classes __declspec(dlle xport).

Best regards,
Vyacheslav Lanovets
Nov 17 '05 #5
Interesting issue; haven't thought of that so far.

Is your clone method virtual or not?

Marcus

"Vyacheslav Lanovets" <xentrax_umail. ru> wrote in message
news:OQ******** ********@tk2msf tngp13.phx.gbl. ..
Hello, Marcus!
You wrote on Thu, 11 Aug 2005 17:44:33 +0200:

MH> I assume I am still missing something here:

MH> <Vyacheslav>
MH> The problem could be solved by moving inline functions to cpp files.
MH> But the class is a template :(
MH> </Vyacheslav>

MH> Why would the problem be solved by moving inline functions to cpp
MH> files?

Because classes with inline constructor (or class templates) have
_different_ vtables when created in different DLLs.

When the class template with virtual function clone is created by code in
module A, it's vtable is in module A address space because its
constructor is "inline".
So, when it's constructed in module B, vtable is in module B address
space.

The problem is that even if I call ->clone() from module B, I still get a
copy with vtable residing in module A, so I can not use Prototype design
pattern if I want to unload module A.

But as soon as I define constructor in x.cpp, the vtable is placed in
module
with x.pp, not in module A or B.

The problem is that this limits usability of templates: in my case I had
to derive all required "instantiations " from class template and declare
constructors of derived classes __declspec(dlle xport).

Best regards,
Vyacheslav Lanovets

Nov 17 '05 #6
On Fri, 12 Aug 2005 00:02:16 +0400, "Vyacheslav Lanovets"
<xentrax_umail. ru> wrote:
Because classes with inline constructor (or class templates) have
_different_ vtables when created in different DLLs.

When the class template with virtual function clone is created by code in
module A, it's vtable is in module A address space because its
constructor is "inline".
So, when it's constructed in module B, vtable is in module B address space.

The problem is that even if I call ->clone() from module B, I still get a
copy with vtable residing in module A, so I can not use Prototype design
pattern if I want to unload module A.

But as soon as I define constructor in x.cpp, the vtable is placed in module
with x.pp, not in module A or B.

The problem is that this limits usability of templates: in my case I had to
derive all required "instantiations " from class template and declare
constructors of derived classes __declspec(dlle xport).


Did you try explicitly instantiating and exporting the class template
specializations you're using? That may allow you to control the placement
of the vtbl.

That said, for DLLs I want to dynamically load and unload, I really tend to
favor a purely C-level interface and black box design, in which the DLL
shares no CRT-level state with the EXE or other DLLs. It's hard enough to
make implicit DLL linking work mostly right for C++ code that's supposed to
behave the same as it would under static linking, without worrying about
DLLs coming and going...

--
Doug Harrison
VC++ MVP
Nov 17 '05 #7
Hello, Doug!
You wrote on Fri, 12 Aug 2005 10:29:09 -0500:

DHM> Did you try explicitly instantiating and exporting the class template
DHM> specializations you're using? That may allow you to control the
DHM> placement of the vtbl.

Yes, it should work, and it's actually been done in other parts of our code,
but only now I could understand - why :)

But dll import/export symbol tables become too large if the entire class is
exported, and it works now, so I'll better leave it as it is.

DHM> That said, for DLLs I want to dynamically load and unload, I really
DHM> tend to favor a purely C-level interface and black box design, in
DHM> which the DLL shares no CRT-level state with the EXE or other DLLs.

Yes, I absolutely agree. But we did not expect to unload those DLLs.
Implicitly loaded they worked fine even with C++ exported classes, templates
and so on.
But we hit 32MB virtual memory limit of Windows CE :(

And, to say you the truth, I'm really surprised that it was so easy to
convert to explicit loading.

Best regards,
Vyacheslav Lanovets
Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
4482
by: SainTiss | last post by:
Hi, Is there an extra overhead in using virtual functions and templates? Or is is just the same overhead as with regular classes? Either way, if you'd want to avoid it, will you always end up with code duplication? For example: let's say you've got two template specializations, which differ only slightly... So you put the common behaviour in a
4
4641
by: Sat | last post by:
Hi, I have a simplified version of a problem that I am facing (hope I haven't oversimplified it). This code doesn't work now and I want to find how I can make it work. Can I call the derived class version from the base class pointer and still be able to use a vector with derived element pointers? class BaseElem { }; class DerivedElem1 { };
1
1318
by: Leslaw Bieniasz | last post by:
Cracow, 15.09.2004 Hi, I am writing a big C++ project using BCB 4.0. The project consists of several dlls and exes. Each of the dll is composed of several units (that is cpp files with corresponding h files) containing: 1) ordinary C++ classes or functions, 2) class templates or function templates. I want the code for templates to reside within the dlls,
25
5409
by: Stijn Oude Brunink | last post by:
Hello, I have the following trade off to make: A base class with 2 virtual functions would be realy helpfull for the problem I'm working on. Still though the functions that my program will use a lot are the ones that are virtual and thus will slow down the calculation, at least that is what what I have read on several places on the internet. It makes sense the program has to work with some kind off lookup table for the virtual...
4
1842
by: Martin | last post by:
Greetings I want to have virtual member functionality, but without my member functions being virtual:-) As of yet this is all just in my head cause I can't see a nice solution yet so lets start with some simple code.
6
3998
by: RainBow | last post by:
Greetings!! I introduced the so-called "thin-template" pattern for controlling the code bloat caused due to template usage. However, one of the functions in the template happens to be virtual as well. To support thin-template, I need to make virtual function as inline. Now, I know that compiler would generate an out-of-line copy when it
4
2006
by: pocmatos | last post by:
Hi all, I have an abstract class acting as interface to a given class of objects. And mostly everywhere around my program I'm passing things like: vector<int>, list<unsigned long>, list<myclass*>, just to access the elements it contains. Today I decided to refactor and to try templates. So I have abstract class A with function foo which receives list<myclass*>. And now I want it to receive an Input Iterator (following the idea behind...
5
3152
by: alind | last post by:
I m having a problem with templates. A per C++ std says i cant use override template virtual functions. in other words virtual functions cant be templates due to some vtable size issues. Now i want to simulate it somehow. I read in some other posts that it is somehow possible throg visitor pattern but i m unable to get it done. My intended code is posted below. can somebody give me a workaround to get this functionality.Any help is appreciated....
5
2952
by: want.to.be.professer | last post by:
For OO design, I like using virtual member function.But considering efficiency, template is better. Look at this program, class Animal { public: virtual void Walk() = 0; }; class Dog
0
8384
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8810
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8659
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6211
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4208
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4387
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2798
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2035
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1790
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.