YAJL内存泄漏问题数组

YAJL内存泄漏问题数组

本文介绍了YAJL内存泄漏问题数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图每2秒获取JSON数据并将其传递给另一个类进行处理,一切正常,但是下面的代码似乎存在内存泄漏(来自Instruments),但是我无法弄清楚什么是错误的,我该怎么办解决,有人可以建议吗?

I am trying to fetch JSON data every 2 seconds and pass it to another class for processing, everything works fine but the below code seems to have memory leaks (from Instruments) but I cannot figure out what is wrong and how I can fix, can someone please advise ???

*已更新为完整逻辑,并且传递给main方法的数组似乎正在泄漏,而Instruments错误地将其报告为YAJL泄漏.你) *

* Updated with the full logic and it looks like the array that is passed on to the main method is leaking and Instruments is falsely reporting that as YAJL leak..(not very sure thou)*

    @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的内存泄漏日志在下面

Memory leak log from Instruments is below


    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:]

推荐答案

嗯,看起来很简单.首先,将JSONData定义为"retain",而不是"assign",然后在后续运行中调用TestMethod时,由于不使用setter,而是直接访问实例变量,因此泄漏了前一个.如果您不需要在其他任何地方使用JSONData,但是只需使用此方法,只需将其定义为局部变量即可,并且无需执行任何特殊操作-它会自动发布.其次-同样的事情发生在testArray上.同样,如果您不需要在其他地方使用它,则将其定义为局部变量,如果需要,则使用setter.

Well, it looks simple.First, you defined your JSONData as 'retain' instead of 'assign', and then upon calling the TestMethod in subsequent runs, you leak the prior one, as you are not using the setter, but rather accessing the instance variable directly.If you don't need the JSONData in any other place, but this method, just define it as a local variable, and don't do anything special - it's autoreleased. And second - the same thing happens to the testArray. Again, if you don't need it in other places, then define as local variable, and if you do, then use the setter.

更新:现在您有一个类似的问题,只是这次是deviceListArray.首先,像这样初始化它:

Update:Now you have a similar problem, just with deviceListArray this time.First, initialize it like this:

self.deviceListArray=[NSMutableArray array];

然后,每次您要分配时,都使用此:

then, every time you want to assign, use this:

self.deviceListArray = newObject;

self.deviceListArray = newObject;

在dealloc中执行

in the dealloc do

[deviceListArray版本];

[deviceListArray release];

这篇关于YAJL内存泄漏问题数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 20:38