Objective C - retain release problem

Discussion in 'Mac Programming' started by quanganhct, Nov 20, 2012.

  1. macrumors member

    Joined:
    Dec 29, 2010
    #1
    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:

  2. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #3
    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.
     
  4. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #4
    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.
     
  5. macrumors 68000

    Joined:
    Jan 28, 2003
    Location:
    Less lost in L.A.
    #5
    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. ;)
     
  6. macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #6

    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.
     
  7. macrumors 6502a

    sinser

    Joined:
    Sep 16, 2003
    #7
    ARC all the way
     
  8. quanganhct, Nov 21, 2012
    Last edited: Nov 21, 2012

    thread starter macrumors member

    Joined:
    Dec 29, 2010
    #8
    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 :)
     
  9. macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #9
    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
     
  10. macrumors G5

    gnasher729

    Joined:
    Nov 25, 2005
    #10
    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.
     

Share This Page