Why does sending retainCount to @“Hi” return -1?

天涯浪子 提交于 2020-01-23 12:41:56
问题 The method retainCount is supposed to return an unsigned integer. Why then, does [@"Hi" retainCount] return -1? 回答1: The simple answer is that because @"Hi " is a string literal, it will always be sitting around in the binary executable image, and will never "go away", thus retain/release have no effect, and you're seeing UINT_MAX (which looks like -1 when printed out signed eg with %d). (See Pete Kirkham's answer about NSObjects having these semantics). Beyond this, it's useful to know that

Why retain count in negative value? [duplicate]

十年热恋 提交于 2020-01-21 11:39:07
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: NSString retain Count Is it possible that any object has its retain count in negative value ? I have this code NSString *str = [[NSString alloc] initWithString:@"Hello World"]; NSLog(@"String Retain Count: %i", [str retainCount]); this will return the retain count -1. Why this happened ? also I have done like this NSString *str = [[NSString alloc] init] still its return negative value in retain count . How this

RetainCount OK to use in this instance?

ⅰ亾dé卋堺 提交于 2020-01-11 10:07:28
问题 RetainCount == BAD retainCount is taboo, unreliable, unpredictable, and in general shouldn't be used. I don't use it anywhere in my code, but I have seen it in one class that I use in an interesting way. I have a class that runs a thread that runs indefinitely until the thread is cancelled. The catch is that the thread increases the retain count of the owner, in my case the class that instantiated it. So, even if I am done using that class, that instance is still going to hang around unless

Reference count is still 1 after [obj release], when it should be deallocated

狂风中的少年 提交于 2019-12-30 11:11:23
问题 When I create an object and check its retain count, I get 1 as expected. When I release the object and then check the retain count again, it is still 1. Shouldn't the object be deallocated, and the retain count 0? NSMutableString *str=[[NSMutableString alloc] initWithString:@"hello"]; NSLog(@"reference count is %i",[str retainCount]); [str release]; NSLog(@"reference count is %i",[str retainCount]); I do see 0 for the retain count if I set str to nil first. Why is that? 回答1: Don't use

RetainCount is -1 after alloc? [duplicate]

拥有回忆 提交于 2019-12-25 18:39:02
问题 This question already has answers here : Calling -retainCount Considered Harmful (2 answers) Closed 6 years ago . Please don't mark Duplicate:- I have created the few lines NSString *str = [[NSString alloc] init]; str = @"Test"; NSLog(@"%d",[str retainCount]); and Output is -1 .Please explain. 回答1: It is a dupe of the question I pointed to. It is -1 because you are printing UINT_MAX as a signed integer. The RC is -1 because that is a singleton string generated by the compiler, effectively,

Should I retain NSString when it points to a literal?

匆匆过客 提交于 2019-12-25 18:14:18
问题 if (url_leng) { NSString *open_string; if (g_system_status.language_code == 0) open_string = @"Open"; else if (g_system_status.language_code == 1) open_string = @"Abrir"; [open_string retain]; [alert addButtonWithTitle : open_string]; g_scan_result = targ_url; } Consider the above code segment. My question is about the "retain" statement. Somehow I need the retain statement to make the code work. My only explanation is when open_string goes out of scope, a release call will be made against it

How to print the retain count of an object?

心已入冬 提交于 2019-12-25 08:55:03
问题 I am trying to print out the retain count of an object in the terminal using NSLog. Here is my code: NSNumber *myInt=[[NSNumber alloc] initWithInteger: 100]; NSLog(@"myInt retain count=%d",[myInt retainCount]); The result should be 1 but what I have got at the terminal is -1. I tried to use %u instead of %d and ended up getting 4294967295 as result. Does anyone know why this happens? 回答1: Before @bbum gets here I get to say this: Don't rely on -retainCount in your code It doesn't give you

How to remove an object from NSMutableArray only when retain count reaches 0?

纵然是瞬间 提交于 2019-12-25 06:34:12
问题 I know I'm not supposed to check or use retainCount, but I'm trying to wonder if there's a way to have an object be removed from an NSMutableArray only after its retain count is 0. Basically, I want to add objects to an array, and have those objects be shared among other windows. When a window uses it, I want the retain count to go up by 1. When it's no longer used, I want it to go down. But, if some window is still using it, then I want it to be available to all other windows. When all

Using 'self' on RxSwift closures… What about instance methods as param?

白昼怎懂夜的黑 提交于 2019-12-20 03:24:15
问题 In other stack overflow questions, it was emphasized that the capture [weak self] should be used for closures that aren't owned by the class because self could be nil before the closure completes. An alternative when the closure is owned by the class itself is [unowned self] . My question is do I need to use [unowned self] when the function I pass as a parameter is an instance method of the current class? Example import RxSwift class Person { var name = "Default name" class func getPersons()

Why retainCount = 2 - after release?

我的未来我决定 提交于 2019-12-18 07:24:16
问题 I use this code. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { // Override point for customization after application launch. view = [[UIView alloc] init]; [_window addSubview:view]; [view release]; NSLog(@"count - %d", [view retainCount]); [self.window makeKeyAndVisible]; return YES; } - (IBAction)click{ NSLog(@"count - %d", [view retainCount]); } When i click to uibutton - my view retain count = 2. Why is this happening? 回答1: