tags:

views:

32

answers:

1

we add an object to the pool and when the pool is drained the objects are flushed out of memory, and if i don't add them into the pool they will remain into the memory and can be accessed after the calling of pool drain method. What i have done in my code is that i have not added the object of my class into the pool and have called the method after the pool drain here's my code

#import <Foundation/Foundation.h>

@interface Myclass : NSObject
{

}

-(void)fun;

@end

@implementation Myclass

-(void)fun
{       
    NSMutableArray *arr = [[NSMutableArray alloc]init];

    char ch[10];
    NSString *str;

    for(int i =0;i<3;i++)
    {           
        scanf("%s",ch);
        str = [NSString stringWithCString:ch];
        [arr addObject:str];
    }

    for(int i =0;i<3;i++)
    {           
        NSLog(@"The values of mutable array are: %@", [arr objectAtIndex:i]);           
    }
}

@end

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Myclass *obj = [[Myclass alloc]init];
        [obj fun];

    // insert code here...
    NSLog(@"\nEnter pool drain");
    [pool drain];
    [obj fun];
    return 0;
}

now when the fun method is called after pool drain each time i add a value to the array i get an error which says

NSautoreleaseNoPool(): Object 0x105a80 of class NSCFString autoreleased with no pool in place - just leaking Stack:(0x4dlfof ox3de442)

but even after this msg is shown i continued to add the data to my array it was working fine but every time showed the above msg. Can you please tell me why is this so?

Also i wanted to know is their any function with the help of which we can clear the console screen i tried ncurses.h but was not able to do that.

Please help me out regarding these two problems

A: 

You don't add any of your objects to the autorelease pool. When it is drained, it is empty. To add objects to the autorelease pool you must autorelease them. e.g. in fun

NSMutableArray *arr = [[[NSMutableArray alloc]init] autorelease];

In main:

Myclass *obj = [[[Myclass alloc]init] autorelease];

With those two modifications, you will see one of two things happen:

  1. on the second [obj fun] after the drain you might get an exception for sending a message to a dealloc'd object.

  2. you might get lucky and the memory for obj is still intact on the second [obj fun] in which case you should see a message posted to the console log saying that arr will leak because there is no autorelease pool in place.

Edit following the comment

This message

NSautoreleaseNoPool(): Object 0x105a80 of class NSCFString autoreleased with no pool in place - just leaking Stack:(0x4dlfof ox3de442)

is occurring because the method invoked by this line:

str = [NSString stringWithCString:ch];

tries to put the string into an autorelease pool before returning it to you. However, you have already drained the only pool you ever had, so the attempt fails. The string will therefore leak.

JeremyP
Jeremy you are absolutely right but in this case i am not using autorelease and both the function call are working correctly of course when i try to insert a string at the second function call then i get a error message as i have mentioned but at last my function gets executed and displays me the new three strings which i have added, so why is this happening jeremy???
Radix