Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 08-03-2011, 03:43 PM   #1 (permalink)
dxm
Registered Member
 
Join Date: Oct 2009
Posts: 23
dxm is on a distinguished road
Default return new object from method - memory management

Hi all,
I've tried searching for this but 'return' & 'object' gets a lot of results...

Anyway, so in a method in a view controller I am creating a new object:

PdfScrollView *scrollView = [[PdfScrollView alloc] init];

and then I'm returning it to the caller:

return scrollView;

I've got some serious memory leaks in the app, and I've traced them to this method.

Is the above code ok from a memory management point of view, assuming the method that receives the object releases it?
Should I be using autorelease here somewhere? Does that work?

thanks for any tips.
dxm is offline   Reply With Quote
Old 08-03-2011, 03:59 PM   #2 (permalink)
Reading the Documentation
 
baja_yu's Avatar
 
Join Date: Sep 2010
Location: 45.255019,19.844908
Posts: 5,414
baja_yu has a spectacular aura about
Default

It depends on the name of the method. Convention is that methods called (or containing) alloc, new, copy or retain return retained objects and the caller is responsible for releasing them. In other cases methods return autoreleased objects.

This is all covered in the Memory Management Programing Guide in the documentation.
baja_yu is offline   Reply With Quote
Old 08-03-2011, 04:04 PM   #3 (permalink)
Registered Member
 
Join Date: Jul 2011
Posts: 241
MattW is on a distinguished road
Default

Quote:
Originally Posted by dxm View Post
Hi all,
I've tried searching for this but 'return' & 'object' gets a lot of results...

Anyway, so in a method in a view controller I am creating a new object:

PdfScrollView *scrollView = [[PdfScrollView alloc] init];

and then I'm returning it to the caller:

return scrollView;

I've got some serious memory leaks in the app, and I've traced them to this method.

Is the above code ok from a memory management point of view, assuming the method that receives the object releases it?
Should I be using autorelease here somewhere? Does that work?

thanks for any tips.
Yes. As long as you make sure that anything calling that function at some point calls release on the pointer it gets, then you won't get a leak.
__________________

Highlight PDF text like no other app: iHighlight (now available for iPad and iPhone!)
-----
Create iPhone lists with no typing: Insta-List
-----
Make spelling fun, and create your own tests: iWillSpell
-----
A fast, elegant flashlight app: Insta-Light
-----


FourSixteen Productions
MattW is offline   Reply With Quote
Old 08-03-2011, 05:12 PM   #4 (permalink)
dxm
Registered Member
 
Join Date: Oct 2009
Posts: 23
dxm is on a distinguished road
Default

Cool, thanks guys.
dxm is offline   Reply With Quote
Reply

Bookmarks

Tags
autorelease, memory management

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 392
15 members and 377 guests
7twenty7, blasterbr, buggen, chiataytuday, dedeys78, dre, fiftysixty, HemiMG, jimmyon122, jonathandeknudt, LEARN2MAKE, nyoe, pungs, tymex, UMAD
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 04:03 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0