Reputation: 14524
Consider the following ObjC code example:
- (void)doStuffWithString:(NSString *)someParam {
// Do stuff with someParam
}
If this code were being executed in a multi-threaded app, would it be a good idea to retain/release someParam
? Specifically, I'm thinking of scenarios in which the passed-in parameter is a singleton object shared by many threads. Is the following safer, for example?
- (void)doStuffWithString:(NSString *)someParam {
[stringParam retain];
// Do stuff with someParam
[stringParam release];
}
Upvotes: 5
Views: 1004
Reputation: 58796
No, it's not the job of individual functions to try and provide thread-safety for parameters.
Somewhere up the stack something passed down the object that is the parameter to "doStuffWithString". This is the code that should guarantee the that object will remain valid through the length of the function call.
Two things to consider;
This thread may also be helpful.
Upvotes: 11