本文介绍了Objective C(++)Insanity - 简单赋值给一个单独的float变量导致{{{CRAZY}}}值在另一个变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

memberA在ClassA的标头中定义。

memberA is defined in the header of ClassA.

memberB在ClassB的标头中定义。

memberB is defined in the header of ClassB.

ClassB是ClassA的子类

ClassB is a subclass of ClassA

在ClassB的实例中,通过简单的赋值设置memberA:

Inside an instance of ClassB, setting memberA via simple assignment:

memberA = 0.05

...还会更改memberB, crazy number - 1028443341.另外,为memberA分配0.05将导致memberA在调试器中显示为5.33083531e-38。

...also changes memberB, but to a crazy number -- 1028443341. Additionally, assigning 0.05 to memberA results in memberA showing up in the debugger as 5.33083531e-38.

两个变量都是浮点型, 。我几乎是certianly做一些noob的错误,但我没有任何线索,它可能是什么。

Both variables are floats, neither is a pointer. I'm almost certianly making some noob mistake, but I don't have any clue what it might be. What sort of screw-up might make it so assigning a value to one variable results in crazy values appearing in two variables?

如果是这样, ** * ** * ** * > * ** * ** *

********* Edit **********

为了得到C ++的成员变量,我做了一些棘手:

I narrowed the problem down to some "trickiness" I'd done in order to get C++ member variables:

感谢所有的想法。这是危险的让一个像这样的低级语言的东西像我一样的noob!这里是问题的所在:

Thanks for all the thoughts folks. It's dangerous letting a noob like me at this low-level language stuff! Here's where the problem was:

@interface LoopyPulser : NSObject{

 float _pulseRate;
 UInt32 tickInterval;
 UInt32 step;
 InMemoryAudioFile * audioFilePlayer;
 #ifdef __cplusplus
  ADSR* env;
  StkFrames* audioFrames;
 # endif
 Pattern * pattern;
 float loopLengthRatio;
 float volume;
}

我在SO上的其他地方看过这个#ifdef __cplusplus业务在头文件中有C ++导入,然后由Obj-C文件导入。看来,我现在这是一个可怕的想法,很可能是我的疯狂bug的原因。如果我删除#ifdef __cplusplus中的成员vars,疯狂消失。

I read about this #ifdef __cplusplus business somewhere else on SO, as a way to have C++ imports in header files which are then imported by Obj-C files. Seems to me now that this is a terrible idea, and most likely the cause of my crazy bug. If I remove the member vars inside the #ifdef __cplusplus, the insanity goes away.

那么,在Obj-C ++中拥有C ++成员变量的最好方法是什么?我可以使用ids可能吗?

So what's the best way to have C++ member variables in Obj-C++? Can I use ids maybe?

推荐答案

听起来像memberA和memberB是类的浮点成员遇到随机内存损坏到你的程序写有一些错误。

Sounds like memberA and memberB are floating point members of a class that is experiencing random memory corruption due to your program being written with some errors.

(1)引用计数错误(如果您不使用GC)可能导致保留计数达到零并且对象被处理,你自己的引用。然后可以重用存储器并导致这个有趣的结果。

(1) Reference counting error (if you're not using GC) could result in the retain count hitting zero and an object being disposed of, that you are still holding your own reference to. Then the memory can be re-used and cause this interesting result.

(2)一些其他指针数学,错误间接或其他C编程或ObjectiveC类型错误(以脚自拍)。

(2) Some other pointer math, faulty indirection, or other C programming or ObjectiveC type mistakes (shoot self in foot).

不要假设这些是唯一的两个被破坏的东西。如何将
放入代码中:

Don't assume these are the only two things that are broken. How about putting thefollowing into your code:

// in your class declaration 1
int Magic1;
float MemberB;
int Magic2;
// same thing in class declaration 2:
int Magic1;
float MemberA;
int Magic2;


// somewhere else like your setup code for each of the two classes:
Magic1 = MAGIC_1;
Magic2 = MAGIC_2;

// somewhere else where the bug occurs
if (Magic1 != MAGIC_1) || (Magic2 != MAGIC_2) { ... do something helpful like NSLog(...)  ... }

这篇关于Objective C(++)Insanity - 简单赋值给一个单独的float变量导致{{{CRAZY}}}值在另一个变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 08:41