YAJL массив проблем утечки памяти - PullRequest
0 голосов
/ 08 мая 2011

Я пытаюсь получать данные JSON каждые 2 секунды и передавать их другому классу для обработки, все работает нормально, но приведенный ниже код имеет утечки памяти (из Instruments), но я не могу понять, что не так и как я могуисправить, может кто-нибудь посоветовать, пожалуйста ???

* Обновлен с полной логикой, и похоже, что массив, который передается методу main, утечка, и Instruments ложносообщая, что в качестве утечки YAJL .. (не очень уверен, что вы) *

    @property (nonatomic,retain,readwrite) NSMutableArray *deviceListArray;


    - (void)viewDidLoad
    {
        [super viewDidLoad];
        deviceListArray=[[NSMutableArray alloc]init];
        [self init];
        TestClass *initiateData = [GetData alloc]init];
        [initiateData startTimer];
        [initiateData release];
    }

    - (id) init{
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(receiveDeviceListNotification:) 
                                                     name:@"devicelist"
                                                   object:nil];
         }

    - (void) receiveDeviceListNotification:(NSNotification *) notification{
            deviceListArray=[notification object];
            [deviceListTable reloadData];

    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {

        return [deviceListArray count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
        [deviceListArray retain]; //CRASHES WITHOUT RETAIN specified here
        static NSString *CellIdentifier = @"Cell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
        cell.textLabel.textColor = [UIColor redColor]; 
        if (cell == nil) {
            cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:CellIdentifier] autorelease];
        }
        [cell.textLabel setText:[deviceListArray objectAtIndex:indexPath.row]]; //CRASHES if i remove the retain on devicelistarray
        return cell;
    }


    @class TestClass;

    @implementation TestClass

    - (void)startTimer:(NSString *)timerstring
    {
        if(timerstring ==@"StartNow")
        {
            NSLog(@"Timer started");
            [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(TestMethod:) userInfo:nil repeats:YES];
        }
        else{
            NSLog(@"string not received");
        }
    }

    -(void)TestMethod:(NSTimer *)Timer {            
        NSTimeInterval timeNow= [NSDate timeIntervalSinceReferenceDate];
        NSData  *JSONData = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://www.example/data.json"]];

        NSArray *testArray=[JSONData yajl_JSON]; //MEMORY LEAK HERE


        if(testArray==nil)
        {
            NSLog(@"Array is nil");
        }
        else
        {
            NSArray *arrayNumberOne=[[testArray valueForKey:@"devicelist"]objectAtIndex:0];
            NSArray *arrayNumberTwo=[testArray valueForKey:@"arrayNumberTwo"];
            NSArray *arrayNumberThree=[testArray valueForKey:@"arrayNumberThree"];        
            float dowloadY=[[arrayNumberTwo objectAtIndex:0]floatValue];
            float uploadY=[[arrayNumberThree objectAtIndex:0]floatValue];

            NSDictionary  *newarrayNumberTwoData=  [NSDictionary dictionaryWithObjectsAndKeys:
                [NSDecimalNumber numberWithInt:timeNow], [NSNumber numberWithInt:0], 
                [NSDecimalNumber numberWithFloat:dowloadY], [NSNumber numberWithInt:1],nil
            ] ;

            NSDictionary  *newarrayNumberThreeData=  [NSDictionary dictionaryWithObjectsAndKeys:
                [NSDecimalNumber numberWithInt:timeNow], [NSNumber numberWithInt:0],
                [NSDecimalNumber numberWithFloat:uploadY], [NSNumber numberWithInt:1],nil
            ] ;

            [[NSNotificationCenter defaultCenter] postNotificationName:@"devicelist" object:arrayNumberOne];

            [[NSNotificationCenter defaultCenter] postNotificationName:@"TestData2" object:newarrayNumberTwoData];
            [[NSNotificationCenter defaultCenter] postNotificationName:@"TestData3" object:newarrayNumberThreeData];
        }
    }

    -(void) dealloc{
        [super dealloc];
    }

    @end

Журнал утечки памяти от приборов ниже

    Leaked Object   #   Address Size    Responsible Library Responsible Frame
    __NSArrayM,569  < multiple >  17.78 KB    MYTESTAPP3  -[YAJLDocument parserDidStartArray:]
    Malloc 80 Bytes,480 < multiple >  37.50 KB    MYTESTAPP3  -[YAJLDocument parserDidStartArray:]
    NSCFString,397  < multiple >  11.44 KB    Foundation  -[NSPlaceholderString initWithBytes:length:encoding:]
    NSCFString,     0x4c1dac0   32 Bytes    Foundation  -[NSPlaceholderString initWithBytes:length:encoding:]

1 Ответ

0 голосов
/ 09 мая 2011

Ну, это выглядит просто. Сначала вы определили ваши JSONData как «retain» вместо «assign», а затем при вызове TestMethod в последующих запусках вы пропускаете предыдущий, так как вы не используете setter, а скорее обращаетесь к переменной экземпляра напрямую. Если вам не нужен JSONData в каком-либо другом месте, кроме этого метода, просто определите его как локальную переменную, и не делайте ничего особенного - он автоматически выпущен. И второе - то же самое происходит с testArray. Опять же, если вам это не нужно в других местах, тогда определите как локальную переменную, а если вам нужно, то используйте установщик.

Обновление: Теперь у вас есть похожая проблема, только с deviceListArray на этот раз. Сначала инициализируйте его так:

self.deviceListArray=[NSMutableArray array];

затем, каждый раз, когда вы хотите назначить, используйте это:

self.deviceListArray = newObject;

в сделке до

[релиз deviceListArray];

...