2013-03-18 37 views
0

我在下面的代碼中面臨崩潰問題(僅在Ad-Hoc構建中)。臨時構建中崩潰問題,同時繪製

- (void)drawPreviewInRect:(CGRect)rect { 
    CGContextRef context = UIGraphicsGetCurrentContext(); 
    CGContextSaveGState(context); 

    CGColorRef strokeColor = [self.delegate.strokeColor CGColor]; 
    CGFloat strokeWidth = self.delegate.strokeWidth; 

    CGFloat x = rect.size.width/2.0f; 
    CGFloat y = rect.size.height/2.0f; 
    CGPoint strokePoint = CGPointMake(x, y); 

    CGContextSetLineCap(context, kCGLineCapRound); 
    CGContextSetLineWidth(context, strokeWidth); 
    CGContextSetStrokeColorWithColor(context, strokeColor); 

    CGContextMoveToPoint(context, strokePoint.x, strokePoint.y); 
    CGContextAddLineToPoint(context, strokePoint.x, strokePoint.y); 
    CGContextStrokePath(context); 

    CGContextRestoreGState(context); 
} 

崩潰日誌顯示如下圖:

Exception Type: EXC_BAD_ACCESS (SIGSEGV) 
Exception Codes: KERN_INVALID_ADDRESS at 0x10000008 
Crashed Thread: 0 

Thread 0 name: Dispatch queue: com.apple.main-thread 
Thread 0 Crashed: 
0 libobjc.A.dylib     0x39f535b0 objc_msgSend + 16 
1 CoreGraphics     0x3237c3ec CGColorRetain + 12 
2 CoreGraphics     0x3237c592 CGGStateSetStrokeColor + 38 

的代碼在開發環境完全沒有(在兩個模擬器和設備)。任何理論?我正在ARC工作。

回答

0

問題出在對象保留和釋放 - 正在由ARC處理。顯然,ARC仍然存在一些問題,尤其是使用UIColor - > CGColor。這個問題在這裏解釋:http://blog.bignerdranch.com/296-arc-gotcha-unexpectedly-short-lifetimes/

我改變了以下幾行代碼:

CGContextSetLineWidth(context, strokeWidth); 
CGContextSetStrokeColorWithColor(context, strokeColor); 

此,它的工作現在:

CGContextSetLineWidth(context, self.delegate.strokeWidth); 
CGContextSetStrokeColorWithColor(context, self.delegate.strokeColor.CGColor); 
0

快速猜測:self.delegate未在init中設置爲nil,這導致CGColor在內存的隨機位上被調用。


的詳細信息:在開發過程中建立的對象變量往往都被設置成漂亮的友好nil值。當你發佈版本時,情況並非如此。像這樣的崩潰很可能是未被初始化爲nil

雖然它可能幾乎在任何地方;因爲一個對象變量'somewhere'指向一個隨機的內存位。

您可以嘗試使用Xcode中的分析工具來查看它是否會提取任何內容。除此之外,你將不得不從其對象崩潰通過它的父母,並檢查一切都很好地初始化。