Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Sergio10

macrumors regular
Original poster
Oct 3, 2007
137
0
Hi,

During 3 days I investigate my code and suggest current source is bad
PHP:
- (int)randomWithRange: (NSInteger) min: (NSInteger) max
{
	return random() % max + min; 
}
. . .
while(YES)
{
	int firstValue = [self randomWithRange: 20: 100];
	int secondValue = [self randomWithRange: 20: 100];
	int resultValue = firstValue + secondValue;
							
	if(resultValue < 100)
	{
		break;
	}
}
So current code crashes about 70% when I run it. But if I write the following code I'll get about 10% of crashes. And this is much better than previous case:
PHP:
- (int)randomWithRange: (NSInteger) min: (NSInteger) max
{
	return random() % max + min; 
}
. . .
while(YES)
{
	int firstValue = [self randomWithRange: 1: 9];
	int secondValue = [self randomWithRange: 1: 9];
	int resultValue = firstValue + secondValue;
							
	if(resultValue < 9)
	{
		break;
	}
}

How to fix this problem?

Thanks
 
I've run the first piece of code a whole bunch of times. No crashes. Perhaps the issue is with some other part of the code? A part you haven't shown us yet? It would help if you could tell us where (which line) the code is crashing.
 
I'd like to know which line crashes too. But debug shows me assembler code when crashes (not function)

Here is crash report:
Process: Test_App [6737]
Path: /Users/serg/Library/Application Support/iPhone Simulator/User/Applications/8EB6AFD5-62E4-442B-A9AE-0F56C54E557B/Test_App.app/Test_App
Identifier: Test_App
Version: ??? (???)
Code Type: X86 (Native)
Parent Process: launchd [65]

Interval Since Last Report: 75 sec
Crashes Since Last Report: 1
Per-App Interval Since Last Report: 0 sec
Per-App Crashes Since Last Report: 1

Date/Time: 2009-05-27 19:13:36.167 +0300
OS Version: Mac OS X 10.5.7 (9J61)
Report Version: 6
Anonymous UUID: F91E3CEE-D4C2-4C45-8BFA-AEFD9FBFC02C

Exception Type: EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000002, 0x0000000000000000
Crashed Thread: 0

Application Specific Information:
iPhone Simulator 2.2 (77.4.9), iPhone OS 2.2.1 (5H11)
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFType sizeWithFont:forWidth:lineBreakMode:]: unrecognized selector sent to instance 0x5501d0'

Thread 0 Crashed:
0 com.apple.CoreFoundation 0x923ebf54 ___TERMINATING_DUE_TO_UNCAUGHT_EXCEPTION___ + 4
1 libobjc.A.dylib 0x953ade3b objc_exception_throw + 40
2 com.apple.CoreFoundation 0x92372a53 CFRunLoopRunSpecific + 4355
3 com.apple.CoreFoundation 0x92372c78 CFRunLoopRunInMode + 88
4 GraphicsServices 0x31566600 GSEventRunModal + 217
5 GraphicsServices 0x315666c5 GSEventRun + 115
6 UIKit 0x30a4eca0 -[UIApplication _run] + 581
7 UIKit 0x30a5a09c UIApplicationMain + 1263
8 Math_Guru_App 0x00002308 main + 102 (main.m:14)
9 Math_Guru_App 0x00002276 start + 54

Thread 1:
0 libSystem.B.dylib 0x908f7286 mach_msg_trap + 10
1 libSystem.B.dylib 0x908fea7c mach_msg + 72
2 com.apple.CoreFoundation 0x9237204e CFRunLoopRunSpecific + 1790
3 com.apple.CoreFoundation 0x92372c78 CFRunLoopRunInMode + 88
4 WebCore 0x32b225c0 RunWebThread + 384
5 libSystem.B.dylib 0x90928155 _pthread_start + 321
6 libSystem.B.dylib 0x90928012 thread_start + 34

Thread 2:
0 libSystem.B.dylib 0x908f7286 mach_msg_trap + 10
1 libSystem.B.dylib 0x908fea7c mach_msg + 72
2 GraphicsServices 0x31569a7e EventReceiveThread + 467
3 libSystem.B.dylib 0x90928155 _pthread_start + 321
4 libSystem.B.dylib 0x90928012 thread_start + 34

Thread 3:
0 libSystem.B.dylib 0x908f7286 mach_msg_trap + 10
1 libSystem.B.dylib 0x908fea7c mach_msg + 72
2 com.apple.CoreFoundation 0x9237204e CFRunLoopRunSpecific + 1790
3 com.apple.CoreFoundation 0x92372c78 CFRunLoopRunInMode + 88
4 com.apple.CFNetwork 0x96abc298 CFURLCacheWorkerThread(void*) + 388
5 libSystem.B.dylib 0x90928155 _pthread_start + 321
6 libSystem.B.dylib 0x90928012 thread_start + 34

Thread 0 crashed with X86 Thread State (32-bit):
eax: 0xa02280f0 ebx: 0x953ade1c ecx: 0xa02271a0 edx: 0x00028000
edi: 0xa0230b40 esi: 0xbffff080 ebp: 0xbfffeb28 esp: 0xbfffeb28
ss: 0x0000001f efl: 0x00000282 eip: 0x923ebf54 cs: 0x00000017
ds: 0x0000001f es: 0x0000001f fs: 0x00000000 gs: 0x00000037
cr2: 0x2ea21000

Binary Images:
0x1000 - 0x17fff +Test_App ??? (???) <a8fc1d6bb51737c54d5c5a6c8f011a26> /Users/serg/Library/Application Support/iPhone Simulator/User/Applications/8EB6AFD5-62E4-442B-A9AE-0F56C54E557B/Math_Guru_App.app/Math_Guru_App
0x46000 - 0x72fff com.apple.SystemConfiguration 1.9.5 (1.9.5) <12ef2c263a3958046a4cfd0ab49a95f3> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/SystemConfiguration.framework/Versions/A/SystemConfiguration
0x8c000 - 0xb0fff +libxslt.1.dylib ??? (???) <ec3ae0462040d333effa987384d1b538> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/usr/lib/libxslt.1.dylib
0xba000 - 0xbdffc +libGFXShared.dylib ??? (???) <2acf8511245e6393dbc7a8107d263584> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/OpenGLES.framework/libGFXShared.dylib
0x112000 - 0x1f3ff7 +libxml2.2.dylib ??? (???) <18f014ed14a8281eedb11b16dbcb2313> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/usr/lib/libxml2.2.dylib
0x224000 - 0x295ff1 +WebKit ??? (???) <d2434c38b3802120b0e0ebcc2f283838> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/PrivateFrameworks/WebKit.framework/WebKit
0x300000 - 0x30aff3 +CoreVideo ??? (???) <327d75ff103c5aeaeeceec2aa0a8e49c> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/PrivateFrameworks/CoreVideo.framework/CoreVideo
0x313000 - 0x34eff9 +libGLImage.dylib ??? (???) <69d908a44ab97e1913cb0b20ab5b99e8> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/OpenGLES.framework/libGLImage.dylib
0x354000 - 0x3d0feb com.apple.audio.CoreAudio 3.2.0 (3.2) <53fe4cee1a8175ea9644c845bb98456d> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/CoreAudio.framework/Versions/A/CoreAudio
0x42f000 - 0x473fe6 +libCGFreetype.A.dylib ??? (???) <369f4b1196d649a14cac5a1f5cf8a6b1> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/CoreGraphics.framework/Resources/libCGFreetype.A.dylib
0x856000 - 0xedbf32 +libGLProgrammability.dylib ??? (???) <9046d2abbaec066c06040423e27b6915> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/OpenGLES.framework/libGLProgrammability.dylib
0x42ca000 - 0x42f1fe7 +libRIP.A.dylib ??? (???) <0e743ddd9ca1edd0431a23f50a627a24> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/CoreGraphics.framework/Resources/libRIP.A.dylib
0x30875000 - 0x30887fff +AppSupport ??? (???) <bef8b343070b5329c2f530d4218aad89> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/PrivateFrameworks/AppSupport.framework/AppSupport
0x30a45000 - 0x30ca8fe6 +UIKit ??? (???) <f4c65f1861a5a6daeaebde942e77b55e> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/UIKit.framework/UIKit
0x30ff3000 - 0x311f8fff com.apple.CoreGraphics 1.359.13 (???) <abf63e270c1801e02fb6954ab6cf5e82> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/CoreGraphics.framework/CoreGraphics
0x31270000 - 0x31285fff +OpenGLES ??? (???) <27b097ed65d3da5eaa09fa8ddfbf0c77> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/OpenGLES.framework/OpenGLES
0x312b2000 - 0x31375ff3 +JavaScriptCore ??? (???) <2c056913644b3a5dc4993cd51837dab0> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/PrivateFrameworks/JavaScriptCore.framework/JavaScriptCore
0x31562000 - 0x3156bffd +GraphicsServices ??? (???) <719d0400bacbca45f43f1568285b4c57> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
0x315d7000 - 0x31692ff7 +ImageIO ??? (???) <0889c4b9b2273a815bcb82b08dbf4977> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/PrivateFrameworks/ImageIO.framework/ImageIO
0x31ad4000 - 0x31aecfff +AddressBook ??? (???) <6c8f6e0977ea207a4befa61ba1330f68> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/AddressBook.framework/AddressBook
0x31dcb000 - 0x31e3ffef +QuartzCore ??? (???) <06cf7e879bfb27a7b5ce0f6c49755c12> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/QuartzCore.framework/QuartzCore
0x325ec000 - 0x32be4fff +WebCore ??? (???) <c5060b1d50c2716c8043b96ec31002b5> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/PrivateFrameworks/WebCore.framework/WebCore
0x33d10000 - 0x33d18ffc +SpringBoardServices ??? (???) <62eed9174adb76974fef4361af5dea2e> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/PrivateFrameworks/SpringBoardServices.framework/SpringBoardServices
0x34882000 - 0x3499efef +AudioToolbox ??? (???) <6d0ef663642ce3dfeb93e221a05cd3dc> /Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator2.2.1.sdk/System/Library/Frameworks/AudioToolbox.framework/AudioToolbox
0x8fe00000 - 0x8fe2db43 dyld 97.1 (???) <100d362e03410f181a34e04e94189ae5> /usr/lib/dyld
0x90009000 - 0x9001ffff com.apple.DictionaryServices 1.0.0 (1.0.0) <ad0aa0252e3323d182e17f50defe56fc> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/DictionaryServices.framework/Versions/A/DictionaryServices
0x9004a000 - 0x90052fff com.apple.DiskArbitration 2.2.1 (2.2.1) <75b0c8d8940a8a27816961dddcac8e0f> /System/Library/Frameworks/DiskArbitration.framework/Versions/A/DiskArbitration
0x90379000 - 0x903a8fe3 com.apple.AE 402.3 (402.3) <b13bfda0ad9314922ee37c0d018d7de9> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/AE.framework/Versions/A/AE
0x908f6000 - 0x90a5dff3 libSystem.B.dylib ??? (???) <b47c8e6e45aef620730710a732bf1930> /usr/lib/libSystem.B.dylib
0x90a7e000 - 0x90b0aff7 com.apple.framework.IOKit 1.5.2 (???) <97b9f9d584f48891377f0481b9104434> /System/Library/Frameworks/IOKit.framework/Versions/A/IOKit
0x91575000 - 0x915a0fe7 libauto.dylib ??? (???) <42d8422dc23a18071869fdf7b5d8fab5> /usr/lib/libauto.dylib
0x91c43000 - 0x91c8cfef com.apple.Metadata 10.5.2 (398.25) <e0572f20350523116f23000676122a8d> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/Metadata.framework/Versions/A/Metadata
0x91c8d000 - 0x91f09fe7 com.apple.Foundation 6.5.8 (677.24) <aa84b0383891378c348ffa4a94853082> /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation
0x91f0a000 - 0x91f0affa com.apple.CoreServices 32 (32) <2fcc8f3bd5bbfc000b476cad8e6a3dd2> /System/Library/Frameworks/CoreServices.framework/Versions/A/CoreServices
0x92080000 - 0x920ddffb libstdc++.6.dylib ??? (???) <04b812dcec670daa8b7d2852ab14be60> /usr/lib/libstdc++.6.dylib
0x920de000 - 0x92198fe3 com.apple.CoreServices.OSServices 227 (227) <30cebcb68b1b571df7d77474e0c31556> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/OSServices.framework/Versions/A/OSServices
0x9227d000 - 0x92284fe9 libgcc_s.1.dylib ??? (???) <f53c808e87d1184c0f9df63aef53ce0b> /usr/lib/libgcc_s.1.dylib
0x922ff000 - 0x92432fef com.apple.CoreFoundation 6.5.6 (476.18) <2d52bab73660117571bd6486dc318f8e> /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation
0x9321f000 - 0x9323dfff libresolv.9.dylib ??? (???) <b5b1527c2d99495ad5d507ab0a4ea872> /usr/lib/libresolv.9.dylib
0x9323f000 - 0x932beff5 com.apple.SearchKit 1.2.1 (1.2.1) <3140a605db2abf56b237fa156a08b28b> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/SearchKit.framework/Versions/A/SearchKit
0x932bf000 - 0x93346ff7 libsqlite3.0.dylib ??? (???) <3334ea5af7a911637413334154bb4100> /usr/lib/libsqlite3.0.dylib
0x93915000 - 0x93919fff libmathCommon.A.dylib ??? (???) /usr/lib/system/libmathCommon.A.dylib
0x953a4000 - 0x95484fff libobjc.A.dylib ??? (???) <7b92613fdf804fd9a0a3733a0674c30b> /usr/lib/libobjc.A.dylib
0x9590e000 - 0x95adfffb com.apple.security 5.0.5 (36371) <c13e0bb1acfdcf94be4d3ee118ef190e> /System/Library/Frameworks/Security.framework/Versions/A/Security
0x95b12000 - 0x95c4bff7 libicucore.A.dylib ??? (???) <cac059ebdac7d9a63ee0f7a648c9f6cf> /usr/lib/libicucore.A.dylib
0x95cc7000 - 0x95cd5ffd libz.1.dylib ??? (???) <5ddd8539ae2ebfd8e7cc1c57525385c7> /usr/lib/libz.1.dylib
0x96475000 - 0x9674fff3 com.apple.CoreServices.CarbonCore 786.11 (786.13) <9e2d85d52e5e2951aa4dd53c48ccc52f> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CarbonCore.framework/Versions/A/CarbonCore
0x968a6000 - 0x968adffe libbsm.dylib ??? (???) <d25c63378a5029648ffd4b4669be31bf> /usr/lib/libbsm.dylib
0x96ab9000 - 0x96b5dfec com.apple.CFNetwork 438.10 (438.12) <fde64bbb20532794396fb21911ccaa98> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/CFNetwork.framework/Versions/A/CFNetwork
0x9722b000 - 0x972b7ff7 com.apple.LaunchServices 290.3 (290.6) <bdba267184df88bd5b8e2116ea903872> /System/Library/Frameworks/CoreServices.framework/Versions/A/Frameworks/LaunchServices.framework/Versions/A/LaunchServices
0xfffe8000 - 0xfffebfff libobjc.A.dylib ??? (???) /usr/lib/libobjc.A.dylib
0xffff0000 - 0xffff1780 libSystem.B.dylib ??? (???) /usr/lib/libSystem.B.dylib
And console log:
5/27/09 7:04:41 PM [0x0-0x23023].com.apple.Xcode[149] Xcode(149,0xb0103000) malloc: free_garbage: garbage ptr = 0x3428b60, has non-zero refcount = 1
5/27/09 7:04:41 PM [0x0-0x23023].com.apple.Xcode[149] Xcode(149,0xb0103000) malloc: free_garbage: garbage ptr = 0x3454cd0, has non-zero refcount = 1
5/27/09 7:04:41 PM [0x0-0x23023].com.apple.Xcode[149] Xcode(149,0xb0103000) malloc: free_garbage: garbage ptr = 0x361dd10, has non-zero refcount = 1
5/27/09 7:04:41 PM [0x0-0x23023].com.apple.Xcode[149] Xcode(149,0xb0103000) malloc: free_garbage: garbage ptr = 0x3641620, has non-zero refcount = 1
5/27/09 7:04:41 PM [0x0-0x23023].com.apple.Xcode[149] Xcode(149,0xb0103000) malloc: free_garbage: garbage ptr = 0x319a140, has non-zero refcount = 1
5/27/09 7:04:41 PM [0x0-0x23023].com.apple.Xcode[149] Xcode(149,0xb0103000) malloc: free_garbage: garbage ptr = 0x34bce40, has non-zero refcount = 1
5/27/09 7:04:42 PM [0x0-0x23023].com.apple.Xcode[149] Xcode(149,0xb0103000) malloc: free_garbage: garbage ptr = 0x361fb50, has non-zero refcount = 1
5/27/09 7:04:42 PM [0x0-0x23023].com.apple.Xcode[149] Xcode(149,0xb0103000) malloc: free_garbage: garbage ptr = 0x364c780, has non-zero refcount = 1
 
Try commenting out the while-loop and see if it's still crashing. Oh, and are you trying to access resultValue outside of the while-loop? If so, you can't. It's instantiated within the while-loop and, therefore, is uninstantiated when the loop ends.
 
The crash log you show isn't related to the code you show. The 'crash' is a runtime exception.

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '*** -[NSCFType sizeWithFont:forWidth:lineBreakMode:]: unrecognized selector sent to instance

Somewhere in your code you're calling sizeWithFont and the object that this is being called on isn't correct. This is usually a memory management bug caused by failure to retain an ivar.

Also, set up a gdbinit file as described in this thread

https://forums.macrumors.com/threads/696095/
 
And also have next crash:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x0000000014b2889b
Crashed Thread: 0

Application Specific Information:
iPhone Simulator 2.2 (77.4.9), iPhone OS 2.2.1 (5H11)

Thread 0 Crashed:
0 libobjc.A.dylib 0x953b9699 objc_msgSend + 41
1 UIKit 0x30b4b2c0 -[UILabel drawTextInRect:] + 70
2 UIKit 0x30a7523d -[UIView(CALayerDelegate) drawLayer:inContext:] + 452
3 QuartzCore 0x31de0f4a -[CALayer drawInContext:] + 65
4 QuartzCore 0x31de0ef3 backing_callback + 69
5 QuartzCore 0x31de0b03 CABackingStoreUpdate + 1986
6 QuartzCore 0x31de017a -[CALayer _display] + 914
7 QuartzCore 0x31dd451d CALayerDisplayIfNeeded + 182
8 QuartzCore 0x31dd288d CAContextCommitTransaction + 191
9 QuartzCore 0x31dd259f CATransactionCommit + 220
10 com.apple.CoreFoundation 0x92370942 __CFRunLoopDoObservers + 466
11 com.apple.CoreFoundation 0x923723a4 CFRunLoopRunSpecific + 2644
12 com.apple.CoreFoundation 0x92372c78 CFRunLoopRunInMode + 88
13 GraphicsServices 0x31566600 GSEventRunModal + 217
14 GraphicsServices 0x315666c5 GSEventRun + 115
15 UIKit 0x30a4eca0 -[UIApplication _run] + 581
16 UIKit 0x30a5a09c UIApplicationMain + 1263
17 Math_Guru_App 0x0000273c main + 102 (main.m:14)
18 Math_Guru_App 0x000026aa start + 54

How to find where exactly crashes???
Thanks


P.S. I did found in my code method called: sizeWithFont
 
I solved the problem. The problem was next:
PHP:
	UILabel *label = [[UILabel alloc] initWithFrame: frame];
	[label autorelease];
	[label setFont:[UIFont systemFontOfSize: 50]];
Thanks to all)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.