概括:
在 UITextField 的子类中:
[UIColor colorWithRed:green:blue:alpha]
导致整个应用程序崩溃[UIColor greenColor]
工作正常,没有任何问题
这怎么可能?
回答:
事实证明,问题layoutSubviews
在于我正在调用正在使用的方法colorWithRed
。显然colorWithRed
分配了额外的内存,这会导致此时出现问题。(感谢 Johannes Fahrenkrug 在评论中指出它很可能是别的东西——见下文。)
细节:
在作为 UITextField 子类的 ECTextField 类中,当我进行以下调用时,我遇到了可怕的崩溃:
[self setTextColor:[UIColor colorWithRed:42/255.0 green:170/255.0 blue:42/255.0 alpha:0.8]];
该应用程序冻结,挂起,然后过了一会儿我收到以下错误:
SampleApp(58375,0x34f71a8) malloc: *** mach_vm_map(size=1048576) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
2013-11-14 22:25:11.929 SampleApp[58375:907] *** Terminating app due to uncaught exception 'NSMallocException', reason: '*** NSAllocateObject(): attempt to allocate object of class 'NSPathStore2' failed'
*** First throw call stack:
(0x2362012 0x2166e7e 0x2361deb 0x1b5bcf2 0x1b60148 0x1bdb7a6 0x120f8a3 0x10ccaf2 0x96b14e4 0x10cc84e 0x96b1542 0x130c42 0x120491 0x120308 0x10fb2dd 0x217a6b0 0x3dbfc0 0x3d033c 0x3d0150 0x34e0bc 0x34f227 0x34f8e2 0x232aafe 0x232aa3d 0x23087c2 0x2307f44 0x2307e1b 0x35647e3 0x3564668 0x10aaffc 0x798d 0x25a5)
libc++abi.dylib: terminate called throwing an exception
奇怪的是,如果我用以下代码替换那行代码,一切正常:
[self setTextColor:[UIColor greenColor]];
有谁知道为什么会这样,我可以尝试解决这个问题吗?
谢谢你的帮助,
埃里克