2011-05-08 3 views
0

2 초마다 JSON 데이터를 가져 와서 처리를 위해 다른 클래스에 전달하려고하면 모든 것이 제대로 작동하지만 아래 코드는 (Instruments의) 메모리 누수가있는 것 같지만 무엇인지 알아 내지 못합니다. 잘못 고칠 수있는 방법, 누군가 제발 할 수있는 조언을 ???YAJL 메모리 누수 문제 배열

* 전체 논리를 업데이트하며 누수되는 주요 방법에 전달 된 배열처럼 보이는 계기가 거짓보고 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 [email protected]"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:] 
+0

두 개의 yajl이 있습니다 - 하나는 C (하스켈 바인딩이 있음)와 하나가 Ruby에 있습니다. 그래서 나는이 루비에 태그를 붙였습니다. –

+0

Instrument (Instrument) 란 무엇입니까? –

+0

@Andrew Grimm 저는 Xcode Link에서 "Instruments"를 사용하고 있습니다 : http : //developer.apple.com/technologies/tools/ – Linus

답변

0

이하입니다. 먼저 JSONData를 'assign'대신 'retain'으로 정의한 다음 후속 실행에서 TestMethod를 호출하면 setter를 사용하지 않고 이전 인스턴스를 유출하지만 인스턴스 변수에 직접 액세스합니다. 다른 곳에서는 JSONData가 필요하지 않지만이 방법은 로컬 변수로 정의하고 특별한 작업을 수행하지 마십시오. 자동 실행됩니다. 그리고 두 번째 - 동일한 것은 testArray에 발생합니다. 다시 말하지만, 다른 곳에서 필요하지 않다면, 지역 변수로 정의하고, 그렇게한다면, 세터를 사용하십시오.

업데이트 : 이번에는 deviceListArray와 비슷한 문제가 발생합니다. 먼저, 다음과 같이 초기화 :

self.deviceListArray=[NSMutableArray array]; 

을 다음 할당 할 때마다,이 사용

self.deviceListArray = NewObject에; 할당 해제에

[deviceListArray 릴리스];

+0

귀하의 의견을 토대로 개정했지만 NSArray "testArray"는 여전히 계측기 – Linus

+0

에 따라 새어 나옵니다. YAJL 버그처럼 보입니다. 코드를 어딘가에 게시 할 수 있다면 그 코드를 살펴볼 수 있습니다. 나는 https://github.com/gabriel/yajl-objc/blob/master/Classes/YAJLDocument.m을 보았지만 명백한 것을 보지 못했다. – Roman

+0

나는 완전한 로직으로 업데이트했으며 main 메소드에 전달 된 devicearray가 누수의 원인이되는 것처럼 보입니다. - (UITableViewCell *)에서 [devicelistarray retail]을 제거하면 tableView : (UITableView *) tableView cellForRowAtIndexPath : (NSIndexPath *) indexPath 메소드가 충돌하여 누수를 일으킬 수 있습니다. 수정하는 방법에 대해 조언 해 주실 수 있습니까?tks – Linus