Code Example: Why can I still access this NSString object after I've released it?

前端 未结 3 1402
生来不讨喜
生来不讨喜 2021-01-20 10:34

I was just writing some exploratory code to solidify my understanding of Objective-C and I came across this example that I don\'t quite get. I define this method and run th

3条回答
  •  有刺的猬
    2021-01-20 11:41

    You're getting a double free error because you are releasing twice and causing two dealloc messages. =P

    Keep in mind that just because you release doesn't doesn't mean the data at its memory address is immediately destroyed. It's just being marked as unused so the kernel knows that, at some point in the future, it is free to be used for another piece of data. Until that point (which is totally nondeterministic in your app space), the data will remain there.

    So again: releasing (and dealloc'ing) doesn't necessitate immediate data destruction on the byte level. It's just a marker for the kernel.

提交回复
热议问题