0

我试图每 2 秒获取一次 JSON 数据并将其传递给另一个类进行处理,一切正常,但下面的代码似乎有内存泄漏(来自 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

Instruments 的内存泄漏日志如下

    Leaked Object # Address Size 责任库责任框架
    __NSArrayM,569 < 多个 > 17.78 KB MYTESTAPP3 -[YAJLDocument parserDidStartArray:]
    Malloc 80 字节,480 < 多个 > 37.50 KB MYTESTAPP3 -[YAJLDocument parserDidStartArray:]
    NSCFString,397 < 多个 > 11.44 KB 基础-[NSPlaceholderString initWithBytes:length:encoding:]
    NSCFString, 0x4c1dac0 32 字节基础-[NSPlaceholderString initWithBytes:length:encoding:]
4

1 回答 1

0

嗯,看起来很简单。首先,您将 JSONData 定义为“retain”而不是“assign”,然后在后续运行中调用 TestMethod 时,您会泄漏前一个,因为您没有使用 setter,而是直接访问实例变量。如果您在任何其他地方不需要 JSONData,但是这个方法,只需将其定义为局部变量,并且不要做任何特殊的事情 - 它是自动释放的。第二 - testArray 也发生了同样的事情。同样,如果您在其他地方不需要它,则定义为局部变量,如果需要,则使用 setter。

更新:现在您遇到了类似的问题,这次只是使用了 deviceListArray。首先,像这样初始化它:

self.deviceListArray=[NSMutableArray array];

然后,每次你想分配,使用这个:

self.deviceListArray = newObject;

在 dealloc 中做

[deviceListArray 发布];

于 2011-05-08T23:27:14.030 回答