2011-03-10 5 views
1

상속 반환 나는 다음과 같은 기능을 가지고 있지만 정의 된 임시 객체의 메모리를 해제하는 방법을 모른다 : 목표 C의 메모리 관리 문제 : NSObject의 클래스 형

#import <UIKit/UIKit.h> 

@interface PatientsSet : NSObject { 
    NSString *tableid; 
    NSString *patient_name; 
    NSString *patient_surname; 
    NSString *city; 
    NSString *State; 
    NSString *phone; 

} 
@property (nonatomic, retain) NSString *tableid; 
@property (nonatomic, retain) NSString *patient_name; 
@property (nonatomic, retain) NSString *patient_surname; 
@property (nonatomic, retain) NSString *city; 
@property (nonatomic, retain) NSString *State; 
@property (nonatomic, retain) NSString *phone; 


-(id)initWithSet:(NSString *)dd patient_name:(NSString *)dn patient_surname:(NSString *)dsn city:(NSString *)ct state:(NSString *)st phone:(NSString *)ph ; 


@end 

(나는으로부터 얻고을 SQLite DB 데이터를 NSobject 파생 클래스로 변환) [set release]를 사용하지 않아야합니다. 어딘가에??

-(PatientsSet *)getPatientById:(NSString *)ID { 
    PatientsSet *set; 
    // Setup the database object 

    sqlite3 *database; 
    // Init the doctors_set Array 
    doctors_set = [[NSMutableArray alloc] init]; 
    //NSString * databasePath1=[ [self getDocumentsDirectory] stringByAppendingPathComponent:databaseName ]; 
    // Open the database from the users filessytem 
    if(sqlite3_open([databasePath UTF8String], &database) == SQLITE_OK) { 
     // Setup the SQL Statement and compile it for faster access 
     NSString* myString = [NSString stringWithFormat:@"SELECT patients.id,patients.patient_name,patients.patient_surname,patients.phone FROM patients where patients.id = '%@'",ID]; 

     const char *sqlStatement = [myString cString]; 

     sqlite3_stmt *compiledStatement; 

     if(sqlite3_prepare_v2(database, sqlStatement, -1, &compiledStatement, NULL) == SQLITE_OK) { 

      // Loop through the results and add them to the feeds array 
      while(sqlite3_step(compiledStatement) == SQLITE_ROW) { 

       // Read the data from the result row 
       NSString *aId = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 0)]; 
       NSString *aName = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 1)]; 
       NSString *aDurname = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)]; 
       NSString *aPhone = [NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)]; 

       // Create a new set object with the data from the database 
       set=[[PatientsSet alloc] initWithSet:aId patient_name:aName patient_surname:aDurname city:@"" state:@"" phone:aPhone]; 



      } 
     } 
     // Release the compiled statement from memory 
     sqlite3_finalize(compiledStatement); 

    } 
    sqlite3_close(database); 

    return set; 
} 

답변

4

당신은 너무 같은 오토 릴리즈 개체를 반환해야합니다 물론

return [set autorelease]; 
+0

내가해야합니다 설정하면 프로브 = 전무 권리를!? – PartySoft

+0

수 있습니다. 그것은 모두 당신이 모든 일을하기를 원하는 방식에 달려 있습니다. 내 생각 엔 네일이 없다면 빈'PatientSet'을 반환하기를 원할 것입니다. 다음과 같이 할 수 있습니다 :'[autorelease]; return set! = nil? set : [[[PatientSet alloc] init] autorelease];'. – FreeAsInBeer