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

fuhrj

macrumors newbie
Original poster
Aug 7, 2009
8
0
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.

Code:
	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?
 
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...
 
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...

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.
 
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.

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.

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

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.
 
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.

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.
 
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.

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.

-Lee
 
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.
 
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.