2013-11-28 2 views
0

일부 객관적인 C 코드를 사용하여이 메소드를 작성했으며 계측기에서 누설이 있음을 알려줍니다. 뿐만 아니라 무료 (문자 *) 충돌이 발생합니다 ...C 코드에서 메모리 누수가 발생했습니다.

도움이 될 것입니다.

주셔서 감사합니다 코드가 보유하고 있지 않습니다

NSArray *keys = @[@"@\"NSMutableArray\""]; 

     //Init result 
     id result = object; 

     //Iterate every key 
     for (id key in [dict allKeys]) { 

      //Convert key to const char 
      const char * c =(const char*)malloc(sizeof(uint32_t)); 
      c = [key cStringUsingEncoding:NSASCIIStringEncoding]; 

      //Use c to see if the class has this property 
      if (class_getProperty([object class], c)) { 

       //get the property 
       objc_property_t property = class_getProperty([result class], c); 

       //Get the property name and type 
       const char *name = (const char*)malloc(sizeof(uint32_t)); 
       const char *type = (const char*)malloc(sizeof(uint32_t)); 

       name = property_getName(property); 
       type =property_copyAttributeValue(property, "T"); 

       //Cast const char to string 
       NSString *pNameString = [NSString stringWithFormat:@"%s",name]; 
       NSString *typeString = [NSString stringWithFormat:@"%s",type]; 

       //Add relationships 
       if ([keys containsObject:typeString]) { 

        //Get array of objects 
        NSArray *relationship = [dict objectForKey:pNameString]; 

        NSMutableArray *allSubObjects = [[NSMutableArray alloc]init]; 
        //Parse each individual object 
        for (NSDictionary *relationshipObj in relationship) { 

         //Create class from relationship 
         Class class = NSClassFromString(pNameString); 

         //Create object 
         id sub = [self makeObject:[[class alloc]init] fromDictionary:relationshipObj]; 

         [allSubObjects addObject:sub]; 
        } 

        [result setValue:allSubObjects forKey:pNameString]; 
       }else{ 
        //If so set the property for the key 
        [result setValue:[dict objectForKey:key] forKey:key]; 
       } 
       free((char*)name); 
       free((char*)type); 
       free(property); 
      }else{ 
       //NSLog(@"%@ did not respond to : %@", result, key); 
      } 

      free((void*)c); 

     } 

     //Return result 
     return result; 
+0

나에게 C처럼 보이지 않는다 ... –

+0

나는 그 C++, 비 객관적인 C stuff (const char *)라고 생각한다. –

+1

어디에서 누출이보고 되었는가? 충돌이 어느 선에 있습니까? 더 많은 정보를 제공해 주셔야합니다. – Sebastian

답변

4

당신이 다음 cStringusingEncoding에 의해 반환 된 것을 확보하려고 어떤 cStringUsingEncoding: 수익률 포인터를 다음 덮어 c를 할당하기 때문에이 충돌 것. 또한 원래 포인터가 유출됩니다.

프롬은이 docscStringUsingEncoding

반환 된 C 문자열 수신기 중 하나가 해제 또는 현재 메모리를 비우고까지까지만 유효 보장

, 둘 중 먼저 일어난다. 이 시간 이후에 C 문자열을 저장해야하는 경우 C 문자열을 복사하거나 getCString : maxLength : encoding :을 사용해야합니다.

+0

감사합니다. 이게 효과가 있는지 볼게. –

관련 문제