[英]Struct has undefined value after being passed into a method
I'm have a weird issue with a pair of GLKVector3 structs. 我对一对GLKVector3结构有一个奇怪的问题。 I get them from two calls to an objc method
axisForIndex:
, then pass them to a C function getContactPoint
to perform a computation. 我通过两次调用objc方法
axisForIndex:
来获取它们,然后将它们传递给C函数getContactPoint
以执行计算。 However, when the argument of both calls to axisForIndex:
are zero, their values "vanish" after I enter the method -- that is, the value of first.x might be .55 outside the method, but it is 0 inside. 但是,当两个对
axisForIndex:
调用的参数均为零时,进入方法后它们的值“消失”-也就是说,方法外部的first.x值可能为.55,内部的值为0。 When I look at them in the debugger, none of their fields are filled in, and when I print description I get something like (GLKVector3) varName = <variable not available>
. 当我在调试器中查看它们时,它们的任何字段均未填写,并且在我打印说明时,我得到的东西类似于
(GLKVector3) varName = <variable not available>
。 However, checking the values in the method calling getContactPoint
before and after it is called shows that they are as they should be. 但是,在调用
getContactPoint
的方法之前和之后对其进行检查,结果表明它们与应有的状态相同。 I'm using Xcode6-Beta6 to compile and run the code. 我正在使用Xcode6-Beta6来编译和运行代码。
Here's the code: 这是代码:
GLKVector3 first = [self axisForIndex: oneAxisIndex]; // the structs I want to use
GLKVector3 second = [other axisForIndex: twoAxisIndex];
//in this method they have garbage values
result.contactLocation = getContactPoint(first,
second,
ptOnEdgeOne,
ptOnEdgeTwo);
//test to see if the result was computed correctly
if (result.contactLocation.x == 0 && result.contactLocation.y == 0 && result.contactLocation.z == 0) {
NSLog(@"%ld,%ld", (unsigned long)oneAxisIndex, (unsigned long)twoAxisIndex);
}
result.interpenetration = bestOverlap;
return result;
Here is the implementation of getContactPoint, the method where the values are undefined. 这是getContactPoint的实现,该方法的值未定义。
GLKVector3 getContactPoint(GLKVector3 axisOne,
GLKVector3 axisTwo,
GLKVector3 ptOnEdgeOne,
GLKVector3 ptOnEdgeTwo) {
GLKVector3 toSt = GLKVector3Subtract(ptOnEdgeOne, ptOnEdgeTwo);
GLfloat dpStaOne = GLKVector3DotProduct(axisOne, toSt);
GLfloat dpStaTwo = GLKVector3DotProduct(axisTwo, toSt);
GLfloat smOne = GLKVector3Length(axisOne);
GLfloat smTwo = GLKVector3Length(axisTwo);
GLfloat dotProductEdges = GLKVector3DotProduct(axisTwo, axisOne);
GLfloat denom = smOne * smTwo - dotProductEdges * dotProductEdges;
GLfloat a = (dotProductEdges * dpStaOne - smTwo * dpStaOne)/denom;
GLfloat b = (smOne * dpStaTwo - dotProductEdges * dpStaOne)/denom;
GLKVector3 nearestPtOnOne = GLKVector3MultiplyScalar(GLKVector3Add(ptOnEdgeOne, axisOne), a);
GLKVector3 nearestPtOnTwo = GLKVector3MultiplyScalar(GLKVector3Add(ptOnEdgeTwo, axisTwo), b);
return GLKVector3Add(GLKVector3MultiplyScalar(nearestPtOnOne, 0.5), GLKVector3MultiplyScalar(nearestPtOnTwo, 0.5));
}
Implementation of axisForIndex. 实现axisForIndex。
-(GLKVector3) axisForIndex: (NSUInteger) index
{
GLKVector4 vec = GLKMatrix4GetColumn(self.transformationMatrix, index);
return GLKVector3Make(vec.x, vec.y, vec.z);
}
The reason you're receiving <variable not available>
in the debugger may be due to compiler optimization. 在调试器中收到
<variable not available>
的原因可能是由于编译器优化所致。
Will you try turning off compiler optimization (Build Setting > Optimization Level) for your build and running the debugger again? 您会尝试为您的构建关闭编译器优化(构建设置>优化级别),然后再次运行调试器吗?
See: https://stackoverflow.com/a/13041747/3367343 参见: https : //stackoverflow.com/a/13041747/3367343
Changing the arguments of getContactPoint to pointers and then dereferencing them inside the method, and passing in pointers to the structs I want solves the issue. 将getContactPoint的参数更改为指针,然后在方法内部对其取消引用,然后将指针传递给我想要的结构即可解决此问题。
GLKVector3 getContactPoint(GLKVector3* axisOnePointer,
GLKVector3* axisTwoPointer,
GLKVector3* ptOnEdgeOnePointer,
GLKVector3* ptOnEdgeTwoPointer) {
GLKVector3 axisOne = *axisOnePointer;
GLKVector3 axisTwo = *axisTwoPointer;
GLKVector3 ptOnEdgeOne = *ptOnEdgeOnePointer;
GLKVector3 ptOnEdgeTwo = *ptOnEdgeTwoPointer;
I would be interested in knowing why this is the case. 我想知道为什么会这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.