Is it better to autorelease or release right after?

后端 未结 3 675
时光说笑
时光说笑 2020-12-20 15:42

There are a lot of cases in which one would alloc an instance, and release it right after it\'s being assigned to something else, which retains it internally.

For ex

3条回答
  •  再見小時候
    2020-12-20 16:13

    I usually go for -release rather than -autorelease whenever possible. This comes from years of experience debugging and enhancing other people's Objective-C code. Code that uses autorelease everywhere makes it harder to debug when an object gets over-released, since the extra release happens far away from the incorrect code.

    It's also the case that many folks use autorelease when they just don't understand how cocoa memory management works. Learn the rules, learn the API, and you'll almost never need to autorelease an object.

    A last minor point is that if you don't need the autorelease behavior, then using autorelease just needlessly adds extra work for your program to do.

提交回复
热议问题