Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

quanganhct

macrumors member
Original poster
Dec 29, 2010
31
0
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.
 

Attachments

  • HelloObjC.zip
    2.2 MB · Views: 122

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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/...onceptual/MemoryMgmt/Articles/MemoryMgmt.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
 

xStep

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
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.
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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.
 

xStep

macrumors 68020
Jan 28, 2003
2,031
143
Less lost in L.A.
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. ;)
 

quanganhct

macrumors member
Original poster
Dec 29, 2010
31
0
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:

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
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
 

gnasher729

Suspended
Nov 25, 2005
17,980
5,565
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.