tags:

views:

91

answers:

3

I'm developing an iPhone application using mixed Obj-C and C++. It seems that sometimes the values of various fields are totally bogus as reported by gdb when stepping from an Obj-C file to a C++ file. For instance, in a method:

int count = 1;

for (int i = 0; i < count; ++i) {

  int x = 0;   // put a breakpoint here to see how many times it gets hit.

}

in this example, sometimes gdb will report a value for 'count' that is other than '1'. it might be 126346 for example. but, stepping through the code, the loop is only iterated once, indicating that the value of 'count' actually was the expected value.

I'm new to Xcode. I probably just missing something basic. But it sucks to doubt your tools. Has anyone else seen oddness in this area? Solved it?

+2  A: 

I have not seen gdb misprint variables as you say - however, if you compile in release instead of Debug, you can run into some wierd things with code being optimized away, or not being able to see some variables...

From what you describe, it almost seems more like you had an uninitialized value for "count". If your code looked like:

int count;

Then count could be pretty much anything and thus sometimes would be 0 - but other times some large random number.

Kendall Helmstetter Gelner
I agree that when the debugger tells you the value of a count is an alarmingly large value, uninitialized (or corrupted) memory is definitely something to look for, but the fact that the loop only iterates once clearly indicates that the value reported by gdb and what the code used during execution are not in agreement.
Eric
It depends though on when you did the output to see the value. Nithin has a good example of this in his response.
Kendall Helmstetter Gelner
A: 

Saw oddness, in this regard and in many others. Never solved.

Sometimes using the GDB console directly helps. The way Xcode wraps GDB is definitely flakey.

Seva Alekseyev
+1  A: 

Are you sure that you are getting the 'value' of count all the time, use NSLog and see the value in console. I think it will always show 1. Also,

int count = 1;// put a breakpoint here to see the value of count, before and after execution of the statement.

for (int i = 0; i < count; ++i) {

  int x = 0;   

}

When the breakpoint is hit, that particular line is not hit, and step throuogh to see the change in the value, initially the value given by the gdb will be some arbitrary values, as the variable is not initialised, and once it gets initialised, the value changes to the new value

Nithin
That seems pretty likely to me... gdb should not ever have trouble displaying the value of an int!
Kendall Helmstetter Gelner
before initializing and after releasing an object, this happens in xcode, may be showing the size, location or something like that allocated for to variable. I'm not sure about which one, but this is the thing happening here,Anyway as far as, this is not troubling our codes, what is the need to go behind that. Once we initialize the variable, it had to work as we intend until we release it, thats all.
Nithin