Re: string pointer


Dave
 

On 1 Mar 2018, at 19:18, Steve Mills <sjmills@...> wrote:


On Mar 1, 2018, at 13:05:37, Quincey Morris <quinceymorris@...> wrote:

— This solution:

NSUInteger c = [self computeFor:42 reason:needReason ? &s : nil];
is AFAIK a bit dangerous. The “computeFor:reason:” method stores an *autoreleased* string pointer into its output parameter, so “s” will end up containing this autoreleased pointer when “needReason” is true. That autoreleased pointer is a ticking time bomb that will explode if it’s still being used somewhere when its autorelease pool is drained (which could be a *lot* later, leading to hard-to-find bug) without being owned somewhere else. Whether this is a problem depends on what you do with “s” next.
I disagree that it's dangerous. It's the standard way code is written when a parameter is passed back by reference. How many times in Apple code do you see something like:
Just because you see it in “Apple Code” doesn’t mean its the best or safest way of doing something. Just look at the quality of code coming out of Apple these days, its awful, in fact just look at the mess that is called XCode!

Autoreleasing bugs are really difficult to find which is why I avoid using AU like the plague!

All the Best
Dave

Join cocoa@apple-dev.groups.io to automatically receive all group messages.