Click here to Skip to main content
15,912,977 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralCopy icon to clipboard.. Pin
J Patel12-Jun-01 12:37
J Patel12-Jun-01 12:37 
GeneralSafely remove files... Pin
12-Jun-01 11:26
suss12-Jun-01 11:26 
GeneralRe: Safely remove files... Pin
Jeff Naber12-Jun-01 11:33
Jeff Naber12-Jun-01 11:33 
GeneralBuilding a linked list inside a DLL before DllMain Pin
Steve The Plant12-Jun-01 11:02
Steve The Plant12-Jun-01 11:02 
GeneralRe: Building a linked list inside a DLL before DllMain Pin
Tomasz Sowinski12-Jun-01 11:20
Tomasz Sowinski12-Jun-01 11:20 
GeneralRe: Building a linked list inside a DLL before DllMain Pin
Tim Deveaux12-Jun-01 14:13
Tim Deveaux12-Jun-01 14:13 
GeneralRe: Building a linked list inside a DLL before DllMain Pin
Steve The Plant12-Jun-01 15:24
Steve The Plant12-Jun-01 15:24 
GeneralRe: Building a linked list inside a DLL before DllMain Pin
12-Jun-01 16:32
suss12-Jun-01 16:32 
The problem is the 'order of construction' problem for global variables.

If the global/static 'cListBuilder1' and 'cListBuilder2' objects are constructed BEFORE the global 'g_list' object, then the calls to 'g_list.AddNode' are most likely failing (because g_list does not yet exist, so you can't add to it). Note that this means you can call a member function of an object that has not yet been constructed!! This is perfectly valid C++ code, but not likely to be what you want. You are probably lucky that you are getting just 'failed function call' - often this behaviour will lead to crashes.

Why would the 'listBuilder' objects be created before 'g_list'? Well, since they are all global objects in different compilation units, the order of construction is undefined - the compiler/linker can schedule the calls to the required constructors in any order they like - as long as all global objects get constructed before the 'main' is called. What makes this kind of problem even worse is that the order of construciton can change simply by adding a new global variable into some file in the project - in this case, the compiler/linker can totaly change the order of global constructions, causing previously "working" code to now fail.

The solution is to use "Create On First Use" semantics for the g_list object. This means you don;t make it a global - you create it the first time it is used (the first call to "AddNode" from a global 'cListBuilder' object constructor, for example. To implement this, put the variable into a function, like this :

cLinkedList& g_List()
{
static cLinkedList theList_;
return theList_;
}

Now, your constructors for the cListBuilder become :

class cListBuilder {
cListBuilder()
{
cData* poData = new CData;
g_list().AddNode("NodeName", poData);
}

};

Since the ListBuilder construtor is now calling a function 'g_list()' rather than a variable 'g_list' you do not have to worry about the order of construction. The rules for static variables inside functions means that the internal 'theList_' object is created only once, and only when the function is called. The function cannot be called without creating the static variable, unlike memeber functions of global variables (which can be called from constructors of other objects before the 'owning' object's constructor!).

I think the reason this was working in an EXE and not the DLL is just blind luck - although, you should note that if all 3 global varaibles are in the same compilation unit (eg, the same cpp file) then the order of construction is normally fixed at the order of the variable declarations (but I'm not sure if the language guarantees this!)

GeneralRe: Building a linked list inside a DLL before DllMain Pin
Steve The Plant12-Jun-01 17:36
Steve The Plant12-Jun-01 17:36 
GeneralExporting classes in a dll that uses MFC Pin
Wes Jones12-Jun-01 9:59
Wes Jones12-Jun-01 9:59 
GeneralText viewer application Pin
Jeff Naber12-Jun-01 9:55
Jeff Naber12-Jun-01 9:55 
GeneralRe: Text viewer application Pin
Jeff Naber12-Jun-01 11:29
Jeff Naber12-Jun-01 11:29 
Generalopen files. Pin
12-Jun-01 8:58
suss12-Jun-01 8:58 
GeneralRe: open files. Pin
Carlos Antollini12-Jun-01 9:05
Carlos Antollini12-Jun-01 9:05 
GeneralI did not understand you !!!! Pin
12-Jun-01 19:17
suss12-Jun-01 19:17 
GeneralRe: open files. Pin
12-Jun-01 9:06
suss12-Jun-01 9:06 
Generalwell done !!!!! It was realy silly misprint! Pin
12-Jun-01 19:25
suss12-Jun-01 19:25 
General*.dsw and Borland Compiler Pin
12-Jun-01 8:19
suss12-Jun-01 8:19 
GeneralRe: *.dsw and Borland Compiler Pin
Tim Deveaux12-Jun-01 12:18
Tim Deveaux12-Jun-01 12:18 
GeneralSerial Port Communication. Pin
John Uhlenbrock12-Jun-01 7:44
John Uhlenbrock12-Jun-01 7:44 
GeneralRe: Serial Port Communication. Pin
John Uhlenbrock12-Jun-01 8:55
John Uhlenbrock12-Jun-01 8:55 
GeneralRe: Serial Port Communication. Pin
Eric Hansen12-Jun-01 9:52
Eric Hansen12-Jun-01 9:52 
GeneralRe: Serial Port Communication. Pin
12-Jun-01 22:38
suss12-Jun-01 22:38 
GeneralRe: Serial Port Communication. Pin
Eric Hansen12-Jun-01 9:49
Eric Hansen12-Jun-01 9:49 
GeneralReplacing file types combo in CFileDialog Pin
12-Jun-01 6:54
suss12-Jun-01 6:54 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.