Jump to content

How to relase NSDictionary


4 posts in this topic

Recommended Posts

Hello all,

I am coding in cocoa for iPhone simulator.

I used..

for(int i = 0 ; i < 5 ; i++)

{

[menuList addObject: [NSDictionary dictionaryWithObjectsAndKeys:

@"sdf",kTitleKey,

@"sfs",kExplainKey,

@"dgd",kWebKey,

@"sdfsf",kPublishDateKey,

nil]];

}

 

Now when i run code then i get console warning:

NSAutoreleaseNoPool(): Object 0x10979c0 of class NSCFDictionary autoreleased with no pool in place - just leaking..

 

It create problem for me some time.

 

Thank you..

Link to comment
Share on other sites

The problem is that no NSAutoreleasePool has not been set up for the current thread (either you have spawned a thread yourself, or this is an iPhone related issue). (I am only an OS XCocoa programmer, I have not messed with iPhone programing yet).

 

So, the object created here: [NSDictionary dictionaryWithObjectsAndKeys:...] is never released, hence the "leak".

 

You should try to set up an NSAutoreleasePool (for your thread) at the beginning of execution like this and then release it when you are done with a loop to free memory or when your program quits:

 

id pool = [NSAutoreleasePool new];

 

// Run your code...

 

[pool release];

 

 

Otherwise, and this might be be better for iPhone as it will free memory as soon as possible, do this instead:

 

 

for(int i = 0 ; i < 5 ; i++)

{

 

id dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:

@"sdf",kTitleKey,

@"sfs",kExplainKey,

@"dgd",kWebKey,

@"sdfsf",kPublishDateKey,

nil]

 

[menuList addObject:dictionary];

 

[dictionary release];

 

}

Link to comment
Share on other sites

Hi bofor,

 

I try first,

 

for(int i = 0 ; i < 5 ; i++)

{

 

id dictionary = [[NSDictionary alloc] initWithObjectsAndKeys:

@"sdf",kTitleKey,

@"sfs",kExplainKey,

@"dgd",kWebKey,

@"sdfsf",kPublishDateKey,

nil]

 

[menuList addObject:dictionary];

 

[dictionary release];

 

}

 

 

But it create same warning problem for me as prevoius post...

 

Then second in,

 

id pool = [NSAutoreleasePool new];

 

// Run your code...

 

[pool release];

 

In which the warninng is gone as i mention in previous post..

 

But this code break after some time..

 

Thank you...

Link to comment
Share on other sites

But it create same warning problem for me as prevoius post...

 

That is not too surprising because Cocoa expects an NSAutoreleasePool to be in place and framework methods could be trying to use it.

 

Then second in,

 

id pool = [NSAutoreleasePool new];

 

// Run your code...

 

[pool release];

 

In which the warninng is gone as i mention in previous post..

 

But this code break after some time..

 

Thank you...

 

Without looking at you code it is hard to say what the problem is, likewise you do not say how this "breaks" the code.

 

You might want to read this short overview on NSAutoreleasePools for help: http://developer.apple.com/documentation/C...oc/uid/20000047

Link to comment
Share on other sites

 Share

×
×
  • Create New...