Last paragraph of Preventing Memory Leaks


#1

I am having trouble understanding the last paragraph of Chapter 20 of Objective-C Programming,
“Following the idea of ownership, now it becomes clear why you need to autorelease the string in your description method: The employee object created the string, but it doesn’t want to own it. It wants to give it away”.

It would be nice if the lines of code were called out. Where exactly does the employee object create a string? Which string? Is it *label? My questions are:

  1. Where does the employee object create the string?
  2. Is this final paragraph refering to: @property (strong)NSString *label; ?
    3 Isn’t the “strong” keyword unnecessary? Isn’t “strong” the default?

Thanks,

Dave


#2

[quote=“David1960”]I am having trouble understanding the last paragraph of Chapter 20 of Objective-C Programming,
“Following the idea of ownership, now it becomes clear why you need to [color=#FF0000]autorelease the string[/color] in your [color=#FF0000]description method[/color]: The employee object created the string, but it doesn’t want to own it. It wants to give it away”.

It would be nice if the lines of code were called out. Where exactly does the employee object create a string? Which string? Is it *label? My questions are:

  1. Where does the employee object create the string?
  2. Is this final paragraph refering to: @property (strong)NSString *label; ?
    3 Isn’t the “strong” keyword unnecessary? Isn’t “strong” the default?

Thanks,

Dave[/quote]
That paragraph is talking about the object created and returned by the description method. Focus on that object only; if you need to extend its lifespan, because it has been autoreleased, then you should retain it.


#3

[quote=“ibex10”][quote=“David1960”]I am having trouble understanding the last paragraph of Chapter 20 of Objective-C Programming,
“Following the idea of ownership, now it becomes clear why you need to [color=#FF0000]autorelease the string[/color] in your [color=#FF0000]description method[/color]: The employee object created the string, but it doesn’t want to own it. It wants to give it away”.

It would be nice if the lines of code were called out. Where exactly does the employee object create a string? Which string? Is it *label? My questions are:

  1. Where does the employee object create the string?
  2. Is this final paragraph refering to: @property (strong)NSString *label; ?
    3 Isn’t the “strong” keyword unnecessary? Isn’t “strong” the default?

Thanks,

Dave[/quote]
That paragraph is talking about the object created and returned by the description method. Focus on that object only; if you need to extend its lifespan, because it has been autoreleased, then you should retain it.[/quote]

Could you provide more details? Which class’s decription method? How is it being auto-released? Can you actually quote the line where it is created and released?


#4

The description method is about half way down on the previous page (page 132). Read from there onward to the bottom of the page. Also in the paragraph you are referring to, the employee should be asset (this error may have confused you).


#5

Good timing on this thread, I was just reading that last paragraph and getting stuck.

So, to confirm, the last paragraph should read:

“Following the idea of ownership, now it becomes clear why you need to autorelease the string in your description method: The asset object created the string, but it doesn’t want to own it. It wants to give it away”.

I first looked in the errata post for mention of this error but did not find it. Is it a confirmed error?

Thanks!


#6

Thanks Ibex, that cleared it up.
Dave