Structs and pointers don't display in watch window???

P

Peter Steele

Say I have a managed C++ app with some code that looks something like this:

void MyMethod()
{
struct Point { int x, y; };

Point* p = new Point;

p.x = 1;
p.y = 2;
....
}

During a debugging session, if I create a watch for the variable p, the
watch window does not display its contents. It says it is an invalid
pointer; Further more, if I create a watch for p->x or p->y, it says the
variable does not exist. If I change my code as follows:

struct Point { int x, y; };
....
void MyMethod()
{
Point* p = new Point;

p.x = 1;
p.y = 2;
....
}

then the Watch window correctly displays the value of p as a expandable
structure with a + sign that lets you see x and y as individual variables.

I also had something as simple as this:

{
int* pi = new int;
...
}

and the watch window displayed this value as invalid.

What's going on here anyway? I've experienced a lot of different cases where
the watch window just won't display the values of pointers and simple
structs, whereas in other cases it has no problems. Any suggestions would be
appreciated.
 
T

Tomas Restrepo \(MVP\)

Peter,
Say I have a managed C++ app with some code that looks something like this:

void MyMethod()
{
struct Point { int x, y; };

Point* p = new Point;

p.x = 1;
p.y = 2;
...
}

During a debugging session, if I create a watch for the variable p, the
watch window does not display its contents. It says it is an invalid
pointer; Further more, if I create a watch for p->x or p->y, it says the
variable does not exist. If I change my code as follows:

struct Point { int x, y; };
...
void MyMethod()
{
Point* p = new Point;

p.x = 1;
p.y = 2;
...
}

then the Watch window correctly displays the value of p as a expandable
structure with a + sign that lets you see x and y as individual variables.

I also had something as simple as this:

{
int* pi = new int;
...
}

and the watch window displayed this value as invalid.

What's going on here anyway? I've experienced a lot of different cases where
the watch window just won't display the values of pointers and simple
structs, whereas in other cases it has no problems. Any suggestions would be
appreciated.

I commented on this a while back on my weblog [1]. Perhaps it might help you
understand a bit what's going on...

[1] http://www.winterdom.com/weblog/archives/000067.html
 
P

Peter Steele

Thanks very much for this info. I suspected it had something to do with
managed/unmanaged code. The pointers in this weblog on the problem will
definitely help.

Peter

Tomas Restrepo (MVP) said:
Peter,
Say I have a managed C++ app with some code that looks something like this:

void MyMethod()
{
struct Point { int x, y; };

Point* p = new Point;

p.x = 1;
p.y = 2;
...
}

During a debugging session, if I create a watch for the variable p, the
watch window does not display its contents. It says it is an invalid
pointer; Further more, if I create a watch for p->x or p->y, it says the
variable does not exist. If I change my code as follows:

struct Point { int x, y; };
...
void MyMethod()
{
Point* p = new Point;

p.x = 1;
p.y = 2;
...
}

then the Watch window correctly displays the value of p as a expandable
structure with a + sign that lets you see x and y as individual
variables.

I also had something as simple as this:

{
int* pi = new int;
...
}

and the watch window displayed this value as invalid.

What's going on here anyway? I've experienced a lot of different cases where
the watch window just won't display the values of pointers and simple
structs, whereas in other cases it has no problems. Any suggestions would be
appreciated.

I commented on this a while back on my weblog [1]. Perhaps it might help
you
understand a bit what's going on...

[1] http://www.winterdom.com/weblog/archives/000067.html
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top