gpt4 book ai didi

objective-c - 为什么此代码在使用 Apple LLVM 编译时会崩溃,但在 LLVM/GCC 中不会崩溃?

转载 作者:行者123 更新时间:2023-12-03 17:55:58 24 4
gpt4 key购买 nike

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

NSRect knobRect = _AFSwitchControlKnobRectForInsetBackground(slotRect, _offset);

因为_offset没有设置。它应该在绑定(bind)方法中使用以下行进行设置:

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

但由于某种原因,LLVM 下似乎没有设置任何内容。我的绑定(bind)调用如下所示:

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

知道为什么控件的状态在 LLVM 下不返回任何内容吗?谢谢!

最佳答案

问题实际上出在上面几行,在对 _AFSwitchControlPartRects 的调用中。

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

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

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

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

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 的堆栈上乱写。缓冲区溢出。

在我看来,这似乎是在破坏 self,所以下一次对 self 的取消引用将会消失。这恰好是_offset的使用。

关于objective-c - 为什么此代码在使用 Apple LLVM 编译时会崩溃,但在 LLVM/GCC 中不会崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13506470/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com