-1

I know that nil object does not received message. but dealloc is not.

exam code is here.


Book *theLordOfTheRing = [[Book alloc] init];

...

NSLog(@"title: %@", theLordOfTheRing.titleName);

 theLordOfTheRing.titleName = nil;

[theLordOfTheRing setTitleName:[theLordOfTheRing.titleName stringByAppendingString:@" vol.4"]];

NSLog(@"title: %@", theLordOfTheRing.titleName);

[theLordOfTheRing.titleName dealloc]; //Build is fine with this line.

----- console----

title: The Fellowship

title: (null)


stringByAppendingString: message is not worked but dealloc is worked.

why does work 'dealloc' to nil object?

rmaddy
  • 298,130
  • 40
  • 468
  • 517
Kernelzero
  • 103
  • 12
  • Your question isn't clear. The last line of code in your question won't do anything. Since `theLordOfTheRing.titleName` is `nil`, the `dealloc` method will not be called. – rmaddy Nov 23 '15 at 04:44

1 Answers1

1

This code will compile and run because you can send messages to objects which are nil. When you send a message to a nil object the app will just continue executing. When you are calling [theLordOfTheRing.titleName dealloc]; dealloc method is not actually being called because titleName is nil. The program is just continue executing.

When you run [theLordOfTheRing setTitleName:[theLordOfTheRing.titleName stringByAppendingString:@" vol.4"]]; you are getting (null) because you sending stringByAppendingString to an object (titleName) that is already nil and the method is not being executed. [theLordOfTheRing.titleName stringByAppendingString:@" vol.4"]; will "return" nil and and setTitleName method will be called with parameter being nil.

You should instead of setting titleName to nil set it to @"" blank string this way stringByAppendingString should work because titleName is still alloc and initialized.

theLordOfTheRing.titleName = @"";

I hope I am able to explain this clearly. Let me know if you have any questions.

Yan
  • 3,806
  • 3
  • 19
  • 41
  • thanks Yan. another q. is [object release]. is this make that object to nil? and then how to dealloc this object? – Kernelzero Nov 23 '15 at 04:55
  • Are you using ARC? If you are i don't think you should be calling [object release]. Why are you trying to dealloc the object? With ARC it will be deallocated when there not going to be any strong pointers to it. – Yan Nov 23 '15 at 04:58
  • ok, this project is not using ARC mode. It is weird but I want to know WHEN is calling dealloc. so i testing this codes now. – Kernelzero Nov 23 '15 at 05:03
  • Haven't worked much with non ARC projects. As far as I understand you should don't have to call dealloc you would call release which intern will call dealloc. You should call release then set the pointer to nil. Check out this question/answer http://stackoverflow.com/questions/1210776/objective-c-difference-between-setting-nil-and-releasing – Yan Nov 23 '15 at 05:08
  • In this case why do you want to release the titleName if you going to append to it later? – Yan Nov 23 '15 at 05:09
  • that reason is when i write NSLog(@"title %@", ~.titleName) after dealloc. but log is well worked. so complicate..I do not explain this situation well. I have to learn memory management system on objective-c more. appreciate! have a nice day YAN. – Kernelzero Nov 23 '15 at 05:27
  • Glad to help. Check out these questions/answers http://stackoverflow.com/questions/3801156/when-not-to-alloc-and-init-an-nsstring http://stackoverflow.com/questions/6068073/problem-with-stringbyappendingstring-and-retain-count You have to figure out what is auto release and what you have to release your self. stringByAppendingString will return an auto releasing string so you don't have to call [yourString release] Happy Coding! – Yan Nov 23 '15 at 15:12
  • 1
    "to an object (titleName) that is already nil" An object cannot be nil. `titleName` is a property of pointer type, whose value is a `nil` pointer, i.e. it doesn't point to any object. – newacct Nov 24 '15 at 02:35