Jump to content

Debugger reports member variables as 0


photo

Recommended Posts

#include <core/unigine.h>

class MyClass{
	int memberVar=1;
	MyClass() {
		int localVar=2;
		log.message("localVar %d memberVar %d", localVar, memberVar);
		breakpoint;
	}
};

int init() {
	MyClass foo=new MyClass();
	return 1;
}

log.message output shows localVar as 2 and memberVar as 1, as expected. 

breakpoint hits, I type 'list' in debugger window:

list

Variables:

MyClass::localVar: int: 2

MyClass::memberVar: int: 0

 

I would expect debugger to report MyClass::memberVar with value of 1, but it reports 0. Why is this?

Link to comment

Hi Adam!

 

You can't initialize member variables that way (think of them as C++ member variables, not C#) and by default all member variables are initialized to zero. That explains why you have such values.

Link to comment

Hi Andrey.

 

Sorry, it's still not clear to me what I did incorrectly vs. what the right way to do this is. Could you clarify by posting wrong vs. right example, or link me to specific place in documentation where this is explained?

 

If my member variable initialization is incorrect, why does log.message show "correct" value of 1 while debugger still shows 0? Whether I did something properly or not, I would expect them to agree.

 

I tried altering MyClass, moving memberVar initialization to constructor instead of declaration:

class MyClass{
	int memberVar;
	MyClass() {
		int localVar=2;
		memberVar=1;
		log.message("localVar %d memberVar %d", localVar, memberVar);
		breakpoint;
	}
};

but this gives me identical behavior as first post -- log.message reports memberVar as 1 while debugger reports it as 0. Please help.

Link to comment

Adam,

 

I checked debugger code and indeed it has a bug. What it's trying to show is namespace and local variables, not instance variables. Since interpreter knows "MyClass" is a namespace, it tries to see "MyClass::memberVar" variable value and prints zero instead.

 

On the other hand, log.message function use instance variable and will print correct value.

 

Thanks again for noticing that!

Link to comment
×
×
  • Create New...