 |
|
| Computers Forum Index » Computer Languages (Objective-C) » Managing Return Values... |
|
Page 1 of 1 |
|
| Author |
Message |
| Jason8... |
Posted: Mon Jul 28, 2008 12:04 am |
|
|
|
Guest
|
Hi:
What's the proper technique for memory management of a return object?
In my simple example below, I'm wondering how to manage the return
value? Should I autorelease it, or not specify alloc or autorelease at
all? How about if SomeClass was created with a convenience method like
SomeClass *instanceOfSomeClass = [SomeClass
initWithData:someDataObject];
- (SomeClass *)myMethod
{
//do some work.
return instanceOfSomeClass;
}
I'm working on a framework and would assume a user would retain the
instance of SomeClass, use it, then release it.
Technically if I don't use alloc, copy or retain on the instance it's
not my problem. But this rule does not speak to autorelease.
Thanks. |
|
|
| Back to top |
|
|
|
| Gregory Weston... |
Posted: Mon Jul 28, 2008 11:51 am |
|
|
|
Guest
|
In article
<d0176ab9-d12a-44ad-9494-5e05bc30472c at (no spam) z11g2000prl.googlegroups.com>,
Jason8 <jason.leach at (no spam) gmail.com> wrote:
Quote: Hi:
What's the proper technique for memory management of a return object?
Depends on the protocol, if any, defined by the library you're using.
Quote: In my simple example below, I'm wondering how to manage the return
value? Should I autorelease it, or not specify alloc or autorelease at
all?
I'll infer that the library you're using is Cocoa. The semantics are
that you as the method author should see to it that the code that
invoked the method is not required to release the object. You can
accomplish that by returning an autoreleased object, or by retaining the
object in some sort of cache (while "do some work" has the potential to
retrieve that object), or by returning an object that was
acquired/created by utility mechanism that already conforms to the
protocol, or anything else as long as the invoker doesn't have to act to
prevent a leak.
Quote: How about if SomeClass was created with a convenience method like
SomeClass *instanceOfSomeClass = [SomeClass
initWithData:someDataObject];
Same thing. Although that's a horrible name for a factory method. I'll
go beyond horrible to outright wrong, in fact.
Quote:
- (SomeClass *)myMethod
{
//do some work.
return instanceOfSomeClass;
}
I'm working on a framework and would assume a user would retain the
instance of SomeClass, use it, then release it.
Technically if I don't use alloc, copy or retain on the instance it's
not my problem. But this rule does not speak to autorelease.
I'm not sure what you mean by that last sentence. Autorelease is a
deferred release.
--
"Harry?" Ron's voice was a mere whisper. "Do you smell something ... burning?"
- Harry Potter and the Odor of the Phoenix |
|
|
| Back to top |
|
|
|
|
|
All times are GMT
The time now is Sun Mar 21, 2010 6:03 pm
|
|