views:

449

answers:

2

I'm writing/debugging an iPhone project that uses a large portion of C++ for image processing. I'm having trouble when I'm using gdb under Xcode because whenever I print values, they are completely out of whack with the true values (comparing
cout << "width" << width << endl; prints out 320, and if I do a
print width
in gdb, I get values like 805300460)

I'm guessing it's a problem with gdb, not with the program, since I don't think I'm overwriting memory and values printed to stdout seem correct. The language is auto-set to be C++.

Looking at this Apple GDB doc it seems like adding this compiler flag might help -gdwarf-2? Not sure where to do so.

Anybody run into this problem? Have any ideas?

+1  A: 

At a quick stab, what optimizations are you using? If you're using some, there's the possibility that the value is being kept in a register and not being written to memory just then, and so you'd be getting whatever value used to be in memory. In this case, you'd get the correct answer through the iostream, since the compiler has to make observable behavior match what the language says, but gdb looks behind the scenes.

David Thornley
Under project > Edit Project Settings > Build > Optimization Level, it was set to "Fastest, Smallest" [-Os]. I changed it to None [-O0], and still have no luck. Thanks for the answer though!
Andres
+1  A: 

In gdb try:

p /d width

Perhaps encoding is different.

I do not think that "dwarfs" params will help. Dwarf params describe intermediate code with source code during compilation. Basically they try to preserve source code during compilation phases. In gdb it allows you to match line number from source code to binary statments. That issue shouldn't have anything to do with variables.

name
Also tried it, but with no luck :(. Thanks for the suggestion! If you can think of anything else, please let me know!
Andres
Try this in c++:int a 12345; .. cout << hex << a << endl;And this in gdb:p /x aIt should return the same values.
name
It returns different values. int a = 1234; cout << hex << a << endl; prints out 4d2p /x a prints out 0x0 (not very good sign).I tried the unmodified opensource project and it seems to be working fine, so it seems to be a setting of some sort. Is there a way to reset gdb settings in Xcode?
Andres
Two more interesting facts: http://openradar.appspot.com/7017295 seems like other people are having a similar problem, although it is only a problem when building a modified version of the program. I tried running the modified version on the simulator, and it worked fine, so it does seem to be an iphone bug. This is frustrating...
Andres