Re: NSDictionary dictionaryWithContentsOfFile in Carbon app
Re: NSDictionary dictionaryWithContentsOfFile in Carbon app
- Subject: Re: NSDictionary dictionaryWithContentsOfFile in Carbon app
- From: "Roni Music" <email@hidden>
- Date: Sun, 8 Oct 2006 20:51:52 +0200
Hi,
Thanks for responding!
----- Original Message -----
From: "Uli Kusterer" <email@hidden>
To: "Roni Music" <email@hidden>
Cc: <email@hidden>
Sent: Sunday, October 08, 2006 12:53 PM
Subject: Re: NSDictionary dictionaryWithContentsOfFile in Carbon app
Am 08.10.2006 um 09:52 schrieb Roni Music:
I have a Carbon application and I have just added my first Cocoa stuff
and have a couple of questions relating to have memory is deallocated.
Everything seems to work fine but I'm curious if I'm doing things
correctly (I'm a newbie with Obj-C and Cocoa).
In short, you're doing it wrong. Autorelease pools are not like Apache
API memory pools. They're not intended to be kept around in instance vars
(they stack, so you really should only create one at the start of a
function and release it at the end).
OK
Just create the pool at the start of any function that calls into Cocoa
and release it before exiting the function. May be a good idea to create
an auto_ptr-like object to take ownership of and release ObjC objects.
Another good idea would be to add ObjC exception handlers, too, see
NS_DURING and NS_HANDLER and NS_ENDHANDLER or @try and @catch -- C++
doesn't understand ObjC exceptions.
If you want to keep any object that you don't own (see <http://
devworld.apple.com/documentation/Cocoa/Conceptual/MemoryMgmt/Tasks/
MemoryManagementRules.html> for details on ownership), you must retain it
before you store it in an ivar and release it once you don't need it
anymore (e.g. before replacing it with a new object when you change its
value and in your destructor). If you already own it, you can just store
it away in an ivar, but you still need to release it.
I want to keep the NSDictionary and NSArray objects as long as my C++ class
exists,
since other member functions also uses it.
Would this modified example be correct?
class foo
{
public:
foo();
~ foo();
void bar(); // this function will be called several times
void ThisFunctionWillUseTheNSArray(); // this function will be called
several times
private:
NSDictionary * m_aDictionary;
NSArray * m_anArray;
// private helper class to handle the NSAutoRelease stuff
class CAutoRelease
{
public:
CAutoRelease()
{
m_pool = [[NSAutoreleasePool alloc] init];
}
~CAutoRelease()
{
[m_pool release];
}
private:
NSAutoreleasePool *m_pool;
};
};
//////////////////////
foo:: foo()
{
m_aDictionary = 0;
m_anArray = 0;
}
foo::~foo()
{
[ m_aDictionary release];
[ m_anArray release];
}
// this function will be called several times
// uses the dictonary and array objects
void ThisFunctionWillUseTheNSArray()
{
CAutoRelease autoReleaseMe;
/*
uses m_aDictionary and m_anArray
*/
}
// this function will be called several times
// creates the dictonary and array objects
void foo::bar()
{
CAutoRelease autoReleaseMe;
.....
[m_aDictionary release]; // OK to release even if it's NULL? (which it is
the first time being called)
aDictionary = [NSDictionary dictionaryWithContentsOfFile:[aString
stringByExpandingTildeInPath]];
[m_aDictionary retain];
[m_anArray release];
anArray = [aDictionary objectForKey: @"aKey"];
[m_anArray retain];
......
//now use the array
}
Rolf
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden