Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Nov 20, 2012, 09:54 PM   #1
quanganhct
macrumors member
 
Join Date: Dec 2010
Objective C - retain release problem

Hello there,

I have some problems concerned retain, release problem. I understand well what those 2 do. But, when I write some code, a really funny situation appear. Actually I write 2 little projects to try out these methods, one run normally

Code:
2012-11-21 10:44:25.472 RetainRelease[1352:a0f] init:retain count of 1
2012-11-21 10:44:25.475 RetainRelease[1352:a0f] Count : 2
2012-11-21 10:44:25.476 RetainRelease[1352:a0f] Count : 1
2012-11-21 10:44:25.476 RetainRelease[1352:a0f] dealloc called. Delete
And one project acted funny.

On the first line where I create an object, init is called and print out retain count, which is :

Code:
2012-11-21 10:34:51.691 HelloObjC[1274:a0f] init: Retain count of 57568
2012-11-21 10:34:51.694 HelloObjC[1274:a0f] Count : 57568
2012-11-21 10:34:51.694 HelloObjC[1274:a0f] Count : 57568
2012-11-21 10:34:51.695 HelloObjC[1274:a0f] Count : 57568
2012-11-21 10:34:51.695 HelloObjC[1274:a0f] Count : 57568
2012-11-21 10:34:51.696 HelloObjC[1274:a0f] Count : 57568
2012-11-21 10:34:51.696 HelloObjC[1274:a0f] Count : 57568
2012-11-21 10:34:51.697 HelloObjC[1274:a0f] Count : 57568
2012-11-21 10:34:51.697 HelloObjC[1274:a0f] Count : 57568
I dont understand why it run like that. Retain count didnt even decrease when release method was called.

I attached my code below, so please check it out.
Attached Files
File Type: zip HelloObjC.zip (2.20 MB, 32 views)
quanganhct is offline   0 Reply With Quote
Old Nov 20, 2012, 11:04 PM   #2
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
You have garbage collection on, so retrain/release are irrelevant. Also, don't use "new". Also, don't depend on retainCount for anything. Cocoa's memory management model depends on the concept of ownership. You own something, or you don't. If you don't own something, and you need to keep it around, you take ownership with retain. If you own something and you're done with it, you send it release to relinquish ownership. There are rules about what methods return objects you own and which return things you do not own. You should read:
http://developer.apple.com/library/m...emoryMgmt.html

If you want to use GC you can (but most don't), ARC is another option that handles the retaining and releasing for you. If you opt for those, no more retain/release. If you go manual, you need to learn the rules and abide by them.

-Lee
lee1210 is offline   0 Reply With Quote
Old Nov 20, 2012, 11:24 PM   #3
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
Quote:
Originally Posted by lee1210 View Post
You have garbage collection on, so retrain/release are irrelevant. Also, don't use "new".
Unless new has an specific issue with garbage collection, there is nothing wrong with using it. It is clear what it does, alloc, and then init, just like the longer form.
__________________
My App: CameraTime - Time lapse photography for novice and advanced users.
xStep is offline   0 Reply With Quote
Old Nov 21, 2012, 07:00 AM   #4
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by xStep View Post
Unless new has an specific issue with garbage collection, there is nothing wrong with using it. It is clear what it does, alloc, and then init, just like the longer form.
lee is right. Not because "new" doesn't work, but because it makes it harder to maintain code. When I want to know all the places where some object is allocated, I search for [SomeClass alloc]. Using a different method, which can only be used with one specific version of init, is not helpful at all.
gnasher729 is offline   0 Reply With Quote
Old Nov 21, 2012, 08:07 AM   #5
xStep
macrumors 68000
 
Join Date: Jan 2003
Location: Lost in Minneapolis
Quote:
Originally Posted by gnasher729 View Post
lee is right. Not because "new" doesn't work, but because it makes it harder to maintain code. When I want to know all the places where some object is allocated, I search for [SomeClass alloc]. Using a different method, which can only be used with one specific version of init, is not helpful at all.
Alloc and new isn't the only way to get a retained object, so all your saying is that in those rare search instances you will reduce your search parameter list. I've never done that search.
xStep is offline   0 Reply With Quote
Old Nov 21, 2012, 12:44 PM   #6
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Quote:
Originally Posted by quanganhct View Post
Hello there,

...

I attached my code below, so please check it out.

Don't use garbage collection. It's deprecated, and will become increasingly unsupported in the future. Use ARC instead.

Also, never call retainCount. It will only mislead you.
Catfish_Man is offline   0 Reply With Quote
Old Nov 21, 2012, 05:48 PM   #7
sinser
macrumors 6502a
 
Join Date: Sep 2003
ARC all the way
sinser is offline   0 Reply With Quote
Old Nov 21, 2012, 08:24 PM   #8
quanganhct
Thread Starter
macrumors member
 
Join Date: Dec 2010
Thanks for your replies, that'll be helped.

But, I'm still learning Objective-C myself, and those errors occurred when I tried and modified some sample codes. The book I read is 'Objective-C on the MAC'. They did say that if I use Leopard or later, there's Garbage Collector to do the memory management for me.

About ARC, I 've just googled it, and it seems that it's only available for Xcode 4., and mine is still 3.

So I'll be happy if you could explain to me what happen to my code ?

I've one more question. In real life, do objective-c programmers use release / retain in their codes ?




Edit: Ok, I understand what lee mean now. I turn garbage collector off, and everything is alright. Still, I want to know if objective-c programmers use release / retain in their codes

Last edited by quanganhct; Nov 21, 2012 at 08:35 PM.
quanganhct is offline   0 Reply With Quote
Old Nov 21, 2012, 09:20 PM   #9
lee1210
macrumors 68040
 
lee1210's Avatar
 
Join Date: Jan 2005
Location: Dallas, TX
Most people use ARC, which lets you largely avoid dealing with memory issues directly. It maintains the speed advantage of manual retain/release while simplifying the life of the programmer. I'm sure there's plenty of code out there that still uses retain/release manually, but I'd wager that new projects are mostly ARC.

So: do objective-c programmers use retain/release? A few do, but most do ARC these days.

-Lee
lee1210 is offline   0 Reply With Quote
Old Nov 22, 2012, 05:21 AM   #10
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by quanganhct View Post
I've one more question. In real life, do objective-c programmers use release / retain in their codes ?
In my experience (writing Objective-C code for a living):

1. Garbage collection is a pain in the **** and not worth the trouble. So you either use retain / release or ARC. One major application that used Garbage collection was Xcode itself and I think it caused a lot of trouble.

2. ARC requires that you use the 10.7 SDK or newer. If your code needs to run on 10.6, then you have to be careful. Running code on an earlier OS than your SDK means you need to know what you are doing. ARC on MacOS X requires 64 bit code. So your code with ARC won't run on a 2006 or possibly early 2007 Mac.

When you write new code in Objective-C, ARC is the way to go. If you have an old existing code base, with lots of old CoreFoundation code (not Objective-C) mixed in, the switch is a bit of work, but obviously you don't have the problem with new code.
gnasher729 is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
how to retain unlimited? T4R06 iPhone 4 Jan 8, 2013 07:31 PM
Problem in detect CGRect - objective-c nxcoder iPhone/iPad Programming 0 Dec 31, 2012 10:15 AM
retain UIColor IDMah iPhone/iPad Programming 11 Aug 21, 2012 03:18 PM
Retain the crispy! JMEANS Apple TV and Home Theater 7 Jul 10, 2012 03:53 PM
Circular release problem with NSTimer... ArtOfWarfare iPhone/iPad Programming 3 Jun 11, 2012 07:04 PM

Forum Jump

All times are GMT -5. The time now is 01:39 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC