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: Dominik Pich <email@hidden>
- Date: Fri, 23 Jul 2004 15:37:43 +0200
ok, that code will leak but that is not a c/objC code mixing issue.
You just never release the allocated objective and therefore it will
never be deallocated.
ObjC using the foundation compensates by having an autorelease pool.
Ill write some code to demonstrate - I am newbie myself and could be
wrong :)
void MyDictAdd( id * objectiveC ) {
[objectiveC retain];
//add it to a list
}
void MyDictRemove( id * obectiveC ) {
//remove from list
[objectiveC release];
}
-(void)Example {
MyClass* obj = [MyClass new]; // new = 2 messages: alloc and init
MyDictAdd( obj );
[obj release];
}
id type only used for explanation
On Jul 23, 2004, at 3:18 PM, 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.