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];
}
The basic premise behind the system is that if you want to hold on to a reference to another object, you need to issue a retain on that object. When you no longer have a use for it, you release it.
An Objective-C method declaration includes the parameters as part of its name, using colons, like this: - (void)someMethodWithValue:(SomeType)value; As with the return type, the parameter type is specified in parentheses, just like a standard C type-cast.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With