Dealloc cause program to hang

Discussion in 'Mac Programming' started by fuhrj, Aug 10, 2009.

  1. fuhrj macrumors newbie

    Aug 7, 2009
    I'm teaching myself Objective-C and all the guides instruct you to deallocate an object once you are finished with it.

    I'm using the Plausible Database wrapper for Sqlite and whenever I run the following code, the program hangs. If I remove the dealloc statement, it runs fine.

    	PLSqliteDatabase *db;
    	@try {
    		db = [[PLSqliteDatabase alloc] initWithPath: @"testDB.sqlite"];
    	} @catch (NSException *e) {
    	} @finally {
    		[db close];
    		[db dealloc];
    Can I safely do away with the dealloc?
  2. Guiyon macrumors 6502a

    Mar 19, 2008
    North Shore, MA
    Under most normal circumstances you should never call dealloc. Instead, release the object and the system will call dealloc if needed. In this case, you do realize the finally block always runs so that immediately after you open the file you are closing it...
  3. fuhrj thread starter macrumors newbie

    Aug 7, 2009
    Ah, thanks. Now I remember why I read so much about dealloc. It was because I was looking into programming for the iPhone and there apparently is no memory mgt.
  4. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    There is still retain/release/autorelease-style reference counting on the iPhone (as far as i know, i would hope so...), with autorelease pools, etc. there just isn't GC.

  5. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    I think you're misreading the guides. They should tell you to release an object once you're finished with it, and ONLY those objects you own (not every object is released when you're done with it). They should NOT tell you to dealloc an object. The only place you should typically call dealloc is when you write a subclass and override its -dealloc to release instance variables owned by the object.

    The definitive reference on when to use release and dealloc is the cocoa memory management guide. You should read it. You can find it by googling for the keywords cocoa memory management guide.

    If you can point to any of these guides that tells you to dealloc, please post a reply, so I can avoid using or recommending that guide.
  6. fuhrj thread starter macrumors newbie

    Aug 7, 2009
    Yes, you are correct in that I am confusing release and dealloc.

    I'm reviewing Apple's Objective-C 2.0 guide which is proving to be very helpful.

    Also, after re-reading Memory Management in COCOA Programming for Mac OS X, it mentions adding a -(void)dealloc section to your class. Where, you release the objects. I believe that was why I was thinking that you had to always dealloc.

    My background is in C# where it's not necessary to explicitly release objects. The GC can determine if it's being used or not and will release them automatically. So this is a new approach.

    Thanks for the tips. I'll look into that.
  7. lee1210 macrumors 68040


    Jan 10, 2005
    Dallas, TX
    As long as you are targeting OS X 10.5 or higher and not the iPhone, you can use GC with objective-C 2.0 and get the behavior you are used to.

  8. gnasher729 macrumors P6


    Nov 25, 2005
    You never call [anObject dealloc] yourself. When you are done with an object, call [anObject release] (after carefully reading and understanding the memory management rules).

    When the retain method detects that the retain count of the object is only 1, then it knows that the object should be destroyed; it calls dealloc for the object just before it is destroyed, and then deallocates the memory and the object is gone. Usually you use the dealloc method to release all the objects that your object is keeping hold of (for example, an NSSet would release all elements in the set), and free all memory allocated for use by the object.

    So dealloc is supposed to do all necessary cleanup just before an object is going to be destroyed.
  9. jared_kipe macrumors 68030


    Dec 8, 2003
    Its funny, one time I had a huge problem with a similar situation.

    in a dealloc method of a class of mine I had a whole bunch of releases on objects I owened and at the end the [super dealloc]; method.

    but as I added more objects I accidentally started putting [myNewObject dealloc]; in instead of [myNewObject release]; and hilarity ensued.

Share This Page