使用Apple LLVM编译时,为什么此代码会崩溃,而LLVM / GCC中却没有?

时间:2012-11-22 05:39:29

标签: objective-c xcode cocoa llvm llvm-gcc

我正在尝试在Xcode 4.5.2中的Apple LLVM下编译此代码:http://code.google.com/p/switchcontrol/source/browse/trunk/code/AFSwitchControl.m。它在使用LLVM / GCC编译时有效,但在第198行切换到Apple LLVM时在mouseDown方法中崩溃:

NSRect knobRect = _AFSwitchControlKnobRectForInsetBackground(slotRect, _offset);

因为没有设置_offset。假设使用以下行在bind方法中设置:

[self setOffset:(CGFloat)[self state]];

但似乎由于某种原因,LLVM下没有设置任何内容。我的绑定调用看起来像:

[control bind:NSValueBinding toObject:self withKeyPath:@"isToggleSwitchOn" options:nil];

为什么控件的状态在LLVM下没有返回任何内容?谢谢!

1 个答案:

答案 0 :(得分:1)

在调用_AFSwitchControlPartRects时,问题实际上是几行。

- (void)mouseDown:(NSEvent *)event {
    NSRect textRect, backgroundRect;
    _AFSwitchControlPartRects([self bounds], &textRect, &backgroundRect);

    NSRect slotRect = _AFSwitchControlInsetBackgroundRect(backgroundRect);
    NSRect knobRect = _AFSwitchControlKnobRectForInsetBackground(slotRect, _offset);

_AFSwitchControlPartRects的第二个参数,& textRect是指向rect的指针。

但是在函数的实现中,该参数应该是指向两个 rects的足够空间的指针。

NS_INLINE void _AFSwitchControlPartRects(NSRect bounds, NSRect *textRects, NSRect *backgroundRect) {
    NSDivideRect(bounds, textRects, backgroundRect, NSWidth(bounds)/5.0, NSMinXEdge);

    textRects[1] = _AFSwitchControlInsetTextRect(NSOffsetRect(textRects[0], NSWidth(*backgroundRect), 0));
    textRects[0] = _AFSwitchControlInsetTextRect(textRects[0]);

当写入textRects [1]时,它会在-mouseDown的堆栈上涂鸦。缓冲区溢出。

在我看来它似乎正在发生破坏自我,所以下一次自我解体会死亡。这恰好是_offset的使用。