# Weird problem in C++ with .NET 2003

## Recommended Posts

I've tryed to port one of my projects from Visual Studio 6 to Visual Studio .NET 2003. All went ok but when I've ran the project there were some crashes (apparently without any reason). All problems were somethilg like that:
class myClass
{
public:
.....
inline int GetValue()         {return myValue;}
.....
protected:
.....
int myValue;

}

.....
.....
int a;
myClass *cls = ... something valid
.....
a = cls->GetValue();
.....

In the watch window myValue inside cls is ok, but a has some random data (most of the times 0xcdcdcdcd). I have lots of inline access functions but only a few are having this problem. The only solution was to remove the "inline" and to move the function's body into a cpp. Am I the only one with this problem ? Or anyone knows any little work around ? Thanks.

##### Share on other sites
are you initializing myValue to something at startup? Debug mode does this for you, while release doesn't.

##### Share on other sites
Quote:
 Original post by c0mas In the watch window myValue inside cls is ok, but a has some random data (most of the times 0xcdcdcdcd).

That looks suspiciously like the default value that the debug release will set in order to catch uninitialized values. Sounds like you aren't calling a constructor, or else you aren't setting myValue within that constructor. More code would help clarify.

CM

##### Share on other sites
Yes, I initialize it and it happends on debug and release too.

In some cases (when I return a pointer) the value is not 0xcdcdcdcd, but a strange value.
Also I've tryed to make the member public and use it directly ... the same result.

One more failed try was to use:
inline void SetGetValue(int &val)         {val = myValue;}

It only worked if I used:

static int g_myValue = 0;.....g_myValue = myValue; // somwhere in myClass.....extern int g_myValue;.....int a;a = g_myValue;

Or if I move the function code inside a .cpp file (and remove the inline modifier).

Im sure that the value is set ok (the debugger shows me the right value, or if a make a trace within "cls" I get the right value). And of course rebuild all does not help.
Also I've tryed to see if it doesn't link with VC6 lib's (I've deleted them and relinked the project) same result.

##### Share on other sites
Try pasting a little more consistent code, for all we know you could be housing evil marsians inside those dots.

As the others said, 0xCDCDCDCD most commonly means that the variable has not been initialized, try explicitly setting it first and so on.

##### Share on other sites
As the others have said, try initializing

myValue

in the class constructor's initializer list.

##### Share on other sites
Thanks, thanks a lot, but I'm more than sure it's initialized.

##### Share on other sites
Well, it's not, unless that's the value you set it to.
Either that or it's not pointing to something that is valid anymore.

##### Share on other sites
Uuuuse the Debugger. See if the pointer is changing. If not, put a data breakpoint on mydata and see where it fires.

##### Share on other sites
Well, I've checked with the debugger.

cls it's ok and I can call any function in it. More than that, if I enter (with the debugger) in a cls->Function, myValue it's ok.

I've just realized that I have another thing to try: I'll check if the address(or offset from the address of cls) of myValue is the same.

##### Share on other sites
That works perfectly fine with me in .NET 2003 so there must be a problem somewhere else in the code...
Like someone said before. If you show more code then it might be easier for us to find your problem.

##### Share on other sites
Well and what part of code should I list :) I have about 150 000 lines of code in that project and lots and lots of inline functions and only a fraction of them are making my life a nightmare.

I'm no beginer I just hope someone else had this problem before.

##### Share on other sites
One more thing I should add to this problem, myClass is in one lib and the code where I use it is in another.

##### Share on other sites
Try showing the entire class definition for myClass.

## Create an account

Register a new account

• ### Forum Statistics

• Total Topics
627715
• Total Posts
2978782

• 9
• 21
• 14
• 12
• 42