0

Below code shows up a memory leak , when profiling.

ContentViewController *dataViewController;
dataViewController = [[ContentViewController alloc]initWithNibName:@"ContentViewController" bundle:nil];

dataViewController.DocumentPath = [self.modelArray objectAtIndex:index];
return dataViewController;

How can i solve this leak, Any idea please help me.

8
  • Use ARC (automatic reference counting)
    – Jonathan.
    Jan 14, 2013 at 9:22
  • you should provide more code; specifically: what you do with the return value?
    – sergio
    Jan 14, 2013 at 9:26
  • 4
    When someone asks how to solve a memory leak, telling them to just use ARC is like telling a person who has a leak in their swimming pool to just use another pool that doesn't leak. You're not informing them on how to solve the problem that they're having. You're telling them to spend time, resources and effort on making a change that doesn't educate them.
    – Jasarien
    Jan 14, 2013 at 9:33
  • 1
    @AnoopVaidya disagree.. there is nothing wrong with not using ARC, as long as you know the manual memory management concepts. And for old school developers, who did the hard work of learning/applying manual memory management concept, and write awesome apps on top of that, ARC doesn't offer anything new or attractive. Jan 14, 2013 at 9:38
  • 1
    @AnoopVaidya That is completely irrelevant. Knowing how a plane works is very important whether you're flying a Wright Brothers plane or a Concord. Just because the concord automates things for you doesn't mean you shouldn't know the concepts of physics that keep the plane in the sky. Knowing how to use Obj-C's memory management is very useful and will help you even when using ARC. There are areas of code which cannot use ARC or might require special bridging considerations and knowing how Manual memory management works is very useful in those situations.
    – Jasarien
    Jan 14, 2013 at 9:46

2 Answers 2

4

If you are not using ARC.

Whenever you return an object from method, Return an autoreleased object:

return [dataViewController autorelease];

I'll suggest using ARC is a good option. Because it's much better than manual memory management. ARC is a compile time feature, it'll add retain, release calls for you automatically when you compile your source code.

3
  • @AnoopVaidya: then possibly the above code is right. There is no mistake in above code in ARC mode.
    – Midhun MP
    Jan 14, 2013 at 9:31
  • Your answer may be right, but not good as long as we are not sure whether OP is using ARC or not Jan 14, 2013 at 9:31
  • @Krishnabhadra: you are correct, I edited my answer. I think there is no issues in above code other than the return statement. That's why I added my answer like that :)
    – Midhun MP
    Jan 14, 2013 at 9:43
1

I feel it's safe to assume the questioner is using manual memory management as opposed to ARC as I don't believe this code would leak under ARC.

Having said that, Midhun MP's answer is correct. The returned object needs to be autoreleased to solve the immediate problem. But I wanted to add some information regarding why this is considered a leak by the profiling tools.

Objective-C uses naming conventions on methods to ascertain their memory management semantics. For example, a method named newPerson would be expected to return an owning reference to an object (that is an object with a retain count of +1). A method named person would be expected to return an autoreleased object, (that is an object without an owning reference).

In the first case, the caller of the method owns the object and is expected to release it when finished. The second case illustrates that the caller needn't worry about releasing the object (since it is not owned).

An easy way I like to use to remember the convention is what I learned as the CARN rule.

  • C opy
  • A lloc
  • R etain
  • N ew

Any methods containing these words will be expected, in Cbjective-C, to return owning references to their returned objects.

So in conclusion, if you are intending to return an owned object from your method, amend its name to include one of the above words, or if not, autorelease your returned object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.