• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
RE: Memory management, when mixing C and ObjC
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

RE: Memory management, when mixing C and ObjC


  • Subject: RE: Memory management, when mixing C and ObjC
  • From: Oliver Donald <email@hidden>
  • Date: Fri, 23 Jul 2004 15:09:00 +0100

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

ObjC doesnt do memory management for you, and won't touch your object's
retain counts unless you tell it to. If you want your C to keep those
objects once your ObjC has 'forgotten' about them, you will need to retain
when you add them to your dictionary, and release when you dispose of the
dictionary.

Your code for adding could look like this...

MyDict* dict;
-(void)Example
{
MyClass* obj = [MyClass new];
char* key = "key";

[obj retain];
MyDictStore( dict, key, obj );
}


When you want to release the dictionary, you would have to ask the
dictionary for object references to every object and [xxx release] each of
them.

Hope that helps (and was correct ;)
Oli





DISCLAIMER: The information contained in this e-mail is confidential and may
be privileged. It is intended for the addressee only. If you are not the
intended recipient, please delete this e-mail immediately. The contents of
this email must not be disclosed or copied without the sender's consent. We
cannot accept any responsibility for viruses, so please scan all
attachments. The statements and opinions expressed in this message are those
of the author and do not necessarily reflect those of the company. The
company does not take any responsibility for the views of the author.
_______________________________________________
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.


  • Prev by Date: Re: Memory management, when mixing C and ObjC
  • Next by Date: AirTunes Details
  • Previous by thread: Re: Memory management, when mixing C and ObjC
  • Next by thread: RE: Memory management, when mixing C and ObjC
  • Index(es):
    • Date
    • Thread