Re: Memory management, when mixing C and ObjC
Re: Memory management, when mixing C and ObjC
- Subject: Re: Memory management, when mixing C and ObjC
- From: Hamish Allan <email@hidden>
- Date: Fri, 23 Jul 2004 15:07:17 +0100
The C compiler does not create objects or dispose of them. Sending an
alloc message to a class causes memory to be malloc()ated for an object
-- part of which holds the retain count, initially set to 1. When an
object receives a release message that takes its retain count to 0, the
memory for that object is free()d. malloc and free are library calls,
beyond the compiler's remit.
If I were you I'd wrap your dict store with Objective-C methods that
send a retain to objects when stored, and a release to objects when
removed (Cocoa's collection classes work this way). Factory methods
that return autoreleased objects are particularly convenient for
creating collections of objects which should not survive longer than
their collection does. You seem to be on the right track with this, but
you need to understand that the compiler has nothing to do with heap
allocation.
Hamish
On Jul 23, 2004, at 14:18, Theodore H. Smith wrote:
The problem, OK look at this pseudo code:
MyDict* dict;
-(void)Example {
MyClass* obj = [MyClass new];
char* key = "key";
MyDictStore( dict, key, obj );
}
Now, what happens to obj? Is it disposed? Is it retained? How can the
compiler know when to dispose or retain it? Do I need to add any code
to deal with disposal/refcounts? If MyDictStore is doing all sorts of
crazy stuff with pointers, I don't think that the compiler can figure
that out.
Is this code better?
MyDict* dict;
-(void)Example {
MyClass* obj = [MyClass new];
char* key = "key";
MyDictStore( dict, key, obj );
SomehowIncrementRefCountOfNSObject( obj );
}
Of course, when the dictionary is disposed, it needs to run through
all the objects to release them.
I dont see the problem... What has casting in C to do with
ObjectiveC's memory management?
RefCount is RefCount and that is all that counts
Of course you need to retain/release and have an autorelase pool.
Regards,
Dominik
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.