swift - how can the UILabel become a CALayer and cause a crash? -
i've got custom uiview
of several instances created inside loop:
let models = cdmymodel.mr_findallsortedby("position", ascending: true) as! [cdmymodel] view in myviews { view.removefromsuperview() } self.myviews.removeall(keepcapacity: true) model in models { let myview = myfaqview(width: cgrectgetwidth(self.view.frame)) myview.titlelabel.text = model.title myview.content.text = model.content myview.titlelabel.sizetofit() self.scrollview.addsubview(myview) self.myviews.append(myview) }
i see crashes in crashlytics in line myview.content.text = model.content
:
according crash assume has memory, don't know how myview have been released @ point.
all happens in viewwillappear:
. removing before has this? assume happens on main thread, shouldn't problem - i'm stuck here.
the crash happens on ios 9.
edit
myfaqview init method:
init(width:cgfloat) { self.width = width super.init(frame: cgrectzero) self.addsubview(self.titlelabel) self.addsubview(self.toggleimageview) self.addsubview(self.separatorview) self.content.clipstobounds = true self.addsubview(self.content) self.translatesautoresizingmaskintoconstraints = false self.clipstobounds = true }
edit
let content:uilabel = { let l = uilabel() l.numberoflines = 0 if let font = uifont(name: "opensans", size: 14) { l.font = font } return l }()
these problems tricky track down.
basically, happening memory corruption. address 0x14f822a0
occupied uilabel
content has been used else, in case calayer
. can verify if crash happens locally entering po 0x14f822a0
in lldb , sure enough output address of type calayer
.
with these errors, although crash line can provide clue, not cause of error. has happened elsewhere.
although swift memory managed, there still pitfalls unwary. have seen 2 principal causes of memory corruption. first retain cycles caused self referencing closures. second - more pertinent problem - views related storyboards , xibs.
if follow through logically, can consider calayer
occupies address space taken uilabel
content. runtime attempts send message object thinks occupies address, , caught swift runtime assert triggers exc_bad_instruction
crash.
now, other object have taken residence @ address, original inhabitant uilabel
content must have been released. why runtime release content? because no longer required, i.e. not subview or property of view still required.
i bet if change content subclass uilabel
, add deinit
method breakpoint, surprised see being unexpectedly deinitialised on. test create type follows:
class debuglabel: uilabel { func deinit { nslog("breakpoint on line here!") } }
then change content's type debuglabel
above.
so why happening? money on having 1 of view properties has been created programmatically being either weak
or unowned
. perhaps had these set using iboutlet
removed forgot remove weak
designator?
check through each , every 1 , sure find cause of problem above. nothing created programatically either using initialiser or uinib
should designated weak
or unowned
.
Comments
Post a Comment