Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 17th, 2008, 12:53 AM
Newbie
 
Join Date: Aug 2008
Posts: 1
Default DLL Forgetting Values?

Sorry if this question has been asked before, but I didn't exactly know how to word my question in the search box to get relevant results.

The app I'm working on determines what DLL to load based on certain criteria. Each DLL implements the same abstract class such as in the example below:

Expand|Select|Wrap|Line Numbers
  1. class MyInterface
  2. {
  3. public:
  4.     void initialize() = 0;
  5.     void doSomething(int i) = 0;
  6.     int getSomeVar() = 0;
  7. }
  8.  
  9. class MyFirstImplementation: public MyInterface
  10. {
  11. public:
  12.     void initialize();
  13.     void doSomething(int i);
  14.     int getSomeVar();
  15. private:
  16.     int _someVar;
  17.     int _anotherVar;
  18. }
By setting a pointer to the base class (in this example, MyInterface), the main app can use the same API across all DLLs. The DLLs each read certain files, store the vital contents in member variables and provide a method to get/set these variables (eg. getSomeVar() returns the contents of _someVar in the example above).

However, I've noticed that the DLLs will "forget" the values in these variables after a while. The first call, immediately after the DLL has loaded the file, everything works. I try and access the variables again at another point in the program (I've made sure that nothing else has accessed the DLL by debugging the program line by line) and the functions begin to return junk values. I've even iterated through every function using for () and found that the second time the functions are called, they begin to return junk (but the first time everything works). No other program is accessing this DLL either.

Has anyone experienced the same or even similar situation? If so, are there any ways to fix it? Any help wold be appreciated. Thanks!
Reply
  #2  
Old August 17th, 2008, 12:07 PM
Moderator
 
Join Date: Mar 2007
Location: Voorschoten, the Netherlands
Age: 52
Posts: 8,455
Default

Possibly your program is suffering from 'slicing'; slicing can happen when you
store an object of a derived class in something that can only hold space for the
base class. The rest or the object will be 'sliced' off.

This behaviour notably happens when you have a vector<BaseClass> and you
want to store DerivedClass objects in it. If this applies to your program better
use a vector<BaseClass*> or vector<BaseClass&> instead.

kind regards,

Jos
Reply
  #3  
Old August 17th, 2008, 04:31 PM
Moderator
 
Join Date: Mar 2007
Location: North Bend Washington USA
Age: 68
Posts: 4,872
Default

Where are your base class virtual functions?

Without them you have no polymorphism at all.

Read this: http://bytes.com/forum/thread793836.html.
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles