Public Group

Class member variables suddenly changing?

This topic is 4131 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

I am encountering a bit of an annoying problem that prevents me from loading a dynamic link library (DLL). At first I thought there was something wrong with the DLL, or that I loaded it wrongly, but later I found out that it isn't loaded at all, which consequently caused functions like GetProcAddress to generate errors... As it turns out, something very strange is happening. I have a class, U2DSystem, and in that class I keep certain private variables for loading that dll. I also have a public function 'createRenderer' that will create a renderer object from the DLL, but first it checks if the DLL is loaded or not, and if it isn't, it loads the DLL. The relevant parts of the class definition look like this:
/* Class Declaration */
class U2DSystem
{
public:
U2DRenderer* createRenderer();

private:
HMODULE m_hDLL;
};

/* Create Renderer function */
U2DRenderer* U2DSystem::createRenderer()
{
if ( !m_hDLL )
{
}

/* Get function addresses and call them etc. */
}


The annoying thing is, that in the constructor I set m_hDLL and m_bLibraryLoaded to NULL and false respectively, but somehow, in the createRenderer function both variables don't have their initial values anymore. Both variables aren't changed anywhere else in the class so I can't use either of them to check if I should load the library or not. I'm really at a loss as to why this happens - usually, variables should keep the value they were constructed with unless they are explicitly changed, right?

Share on other sites
Sounds like memory corruption to me. If you're using MSVC, try putting a memory watchpoint on one of the variables and see what's changing it. It's probably something like running off the end of a buffer.

Share on other sites
The problem with debugging is that the class of which I spoke is part of a static library project, and I use another test application to run the library. The static library project contains the winmain function and a prototype for an arbitrary 'main' function that the user of said library will have to implement. Thus, an instance of U2DSystem is created in the library, and right at the beginning of the test application those values are already 'corrupted'...

I wouldn't know how to debug a static library though...

Edit: I just 'debugged' the static library with that test application. Oddly enough, right after creating the object the member variables are corrupted already... It seems as though anything the constructor says is just 'forgotten' by the class...

Edit 2: I think I located the problem. U2DSystem has two constructors, and one of the constructors calls the other (overloaded) constructor. I know that this is allowed in Java, but apparently C++ has problems with it or I'm not doing it correctly... In any case, the issue is solved now...

[Edited by - rogierpennink on March 26, 2007 5:25:01 AM]

Share on other sites
Yeah, a constructor is not just a regular function, so you can't use them in the method you describe. Delegate any such shared initialisation to a separate function.

Share on other sites
just to clarify, what happens in c++ when you try to call another constructor is you actually create an unamed temporary variable...

example:
struct foo{    foo()     {        // this actually creates a foo with 1        // this lets you do stuff like my_func(foo(1))         // but prevents you delegating constructors        foo(1);    }    foo(int) { }};

Share on other sites
Or you could try reading this. :)

Share on other sites
Thanks for the article, Zahlman - I took the default variable approach on this one and it works like a charm. More importantly though, I understand why I'm doing it now :P (That is, I don't understand the underlying magic that constructors perform, I just know how to avoid the trouble it gave me from now on...)

1. 1
2. 2
Rutin
23
3. 3
JoeJ
20
4. 4
5. 5
gaxio
13

• 24
• 40
• 23
• 13
• 13
• Forum Statistics

• Total Topics
631735
• Total Posts
3001934
×