Memory Leak Multi Level xml Parsing Trace Issues into Iphone Application
<p>I am trying to parsing a Multi level Xml File and also Insert xml content into Sqldatabase. </p>

<p>I am Using this Concept. </p>

<p>1 - I am creating class which define me xml tags, and init those tag values as @"null" string value. 
like this </p>

<pre><code>@implementation lifeline_detail
@synthesize lifeline_title,lifeline_identifier,created_date,lifeline_image,lifeline_modifienddate;
-(void) lifeline_deatiail_values
{
    lifeline_title=@"null";
    lifeline_identifier=@"null";
    created_date=@"null";
    lifeline_image=@"null";
    lifeline_modifienddate=@"null";

}
-(void) lifeline_title : (NSString *) xml_value
{lifeline_title=xml_value;

}
-(void) lifeline_identifier : (NSString *) xml_value
{
    lifeline_identifier=xml_value;

}
-(void) created_date : (NSString *) xml_value
{
    created_date=xml_value;

}
-(void) lifeline_modifienddate : (NSString *) xml_value
{
    lifeline_modifienddate=xml_value;

}
-(void) lifeline_image : (NSString *) xml_value
{
    lifeline_image=xml_value;
}
-(void) dealloc
{
    [lifeline_image release];
    [lifeline_title release];
    [created_date release];
    [lifeline_modifienddate release];
    [lifeline_identifier release];
    [super dealloc];
}
@end
</code></pre>

<p>2- I am import this class into my  other class which is using for parse xml file and insert the data into sqlite database. </p>

<p>that call is like that </p>

<p>.h File </p>

<pre><code>#import &lt;Foundation/Foundation.h&gt;
#import &lt;UIKit/UIKit.h&gt;
#import &lt;sqlite3.h&gt;

#import "lifelineemail.h"
#import "lifeline_detail.h"
#import "lifelinephoneno.h"
#import "lifelineaddress.h"
#import "lifeline_geolocation.h"

@class lifeline_geolocation;
@class lifelineaddress;
@class lifelinephoneno;
@class lifelineemail;
@class lifeline_detail;

@class DBAcess;



@interface lifelinexml_parse : NSObject&lt;NSXMLParserDelegate&gt;
{
    sqlite3* database;
    lifelineemail *life_line_email;
    lifelineaddress *life_line_address;
    lifeline_detail *life_line_detail;
    lifelinephoneno *life_line_phone;
    lifeline_geolocation *life_line_geo;
    DBAcess *mydatabasepath;
    NSMutableString *currentValue;
    NSURL *xmlFile;
}
@property (retain,nonatomic) NSURL *xmlFile;

@property (retain,nonatomic) NSMutableString *currentValue;
- (BOOL) parse;
@end
</code></pre>

<p>.m file </p>

<pre><code>#import "lifelinexml_parse.h"
#import "DBAcess.h"




@implementation lifelinexml_parse
@synthesize currentValue,xmlFile;

NSString *xmlPath;



NSString *currentText;
NSString *path;

NSXMLParser *parser;
BOOL parse_finished=NO;


- (void)parser:(NSXMLParser *)parser parseErrorOccurred:(NSError *)parseError {
    NSString * errorString = [NSString stringWithFormat:@"Unable to Parse life line XML"];
    NSLog(@"error parsing XML: %@", errorString);

    UIAlertView * errorAlert = [[UIAlertView alloc] initWithTitle:@"Error Parse content" message:errorString delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
    [errorAlert release];
}


-(void)parserDidStartDocument:(NSXMLParser *)parser
{
    if(mydatabasepath)
    {
        [mydatabasepath release];
    }
    else if(!mydatabasepath)
    {
        mydatabasepath =[[DBAcess alloc] init];
    }

        path=[mydatabasepath initializeDatabase];   





}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qualifiedName attributes:(NSDictionary *)attributeDict
{  


  if ([elementName isEqualToString:@"poi:contact"]) 
      {
          if(life_line_email)
          {
              [life_line_email release];
              life_line_email =[[lifelineemail alloc] init];
              [life_line_email email_initialization];
          }
          else if(!life_line_email)
          {
              life_line_email =[[lifelineemail alloc] init];
              [life_line_email email_initialization];
          }
          if(life_line_address)
          {
              [life_line_address release];
              life_line_address=[[lifelineaddress alloc] init];
              [life_line_address address_initialization];
          }
          else if(!life_line_address)
          {
              life_line_address=[[lifelineaddress alloc] init];
              [life_line_address address_initialization];
          }
          if(life_line_detail)
          {
              [life_line_detail release];
              life_line_detail=[[lifeline_detail alloc] init];
              [life_line_detail lifeline_deatiail_values];

          }
          else if(!life_line_detail)
          {
              life_line_detail=[[lifeline_detail alloc] init];
              [life_line_detail lifeline_deatiail_values];

          }

          if(life_line_phone)
          {
              [life_line_phone release];
               life_line_phone=[[lifelinephoneno alloc] init];
              [life_line_phone phone_initialization];
          }
          else if(!life_line_phone)
          {
              life_line_phone=[[lifelinephoneno alloc] init];
              [life_line_phone phone_initialization];
          }
          if(life_line_geo)
          {
              [life_line_geo release];
              life_line_geo=[[lifeline_geolocation alloc] init];
              [life_line_geo geo_initialization];
          }
          else if(!life_line_geo)
          {
              life_line_geo=[[lifeline_geolocation alloc] init];
              [life_line_geo geo_initialization];
          }






    }
     if ([elementName isEqualToString:@"xal:AddressDetails"]) 
     {
        if ([[attributeDict valueForKey:@"AddressType"] length] !=0)
        {
            [life_line_address address_address_type:[attributeDict valueForKey:@"AddressType"]];    
        }

     }
     if ([elementName isEqualToString:@"poi:email"]) 
        {
        if ([[attributeDict valueForKey:@"email-type"]  length] !=0)
        {
            [life_line_email email_type:[attributeDict valueForKey:@"email-type"]];

        }

       }
  if ([elementName isEqualToString:@"poi:phone-number" ]) 
    {
        if ([[attributeDict valueForKey:@"phone-type"]  length] !=0)
        {
            [life_line_phone phone_type:[attributeDict valueForKey:@"phone-type"]]; 
        }

    }



}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string  
{


    if(!currentValue)
    {
        currentValue =[[NSMutableString alloc] initWithString:string];

    }

    [currentValue appendString:string];
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName 
{

    if ([elementName isEqualToString:@"dc:title"]) 

    { 
        NSString* aString = [NSString stringWithFormat: @"%@", currentValue];

        aString = [aString stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString length]!=0)
        {
            [life_line_detail lifeline_title:aString];

        }
    }
    else if ([elementName isEqualToString:@"dc:identifier"]) 
        {
            NSString* aString1 = [NSString stringWithFormat: @"%@", currentValue];

            aString1 = [aString1 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

            if ([aString1 length]!=0)
            {
                [life_line_detail lifeline_identifier:aString1];
                //[aString1 release];

            }

        }
    else if ([elementName isEqualToString:@"poi:avatar"]) 
    {
        NSString* aString88 = [NSString stringWithFormat: @"%@", currentValue];

        aString88 = [aString88 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([aString88 length]!=0)
        {
            [life_line_detail lifeline_image:aString88];
            //[aString88 release];

        }

    }

    else if ([elementName isEqualToString:@"m:created"]) 
    {
        NSString* aString2 = [NSString stringWithFormat: @"%@", currentValue];

        aString2 = [aString2 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString2 length]!=0)
        {
            [life_line_detail created_date:aString2];
            //[aString2 release];


        }


    }


    else if ([elementName isEqualToString:@"m:modified"]) 
    {
        NSString* aString3 = [NSString stringWithFormat: @"%@", currentValue];

        aString3 = [aString3 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([aString3 length]!=0)
        {
            [life_line_detail lifeline_modifienddate:aString3];
            //[aString3 release];


        }



    }

    else if ([elementName isEqualToString:@"geo:lat"]) 
    {
        NSString* aString4 = [NSString stringWithFormat: @"%@", currentValue];

        aString4 = [aString4 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([aString4 length]!=0)
        {
            [life_line_geo geo_lat_init:aString4];
            //[aString4 release];


        }


        //[[self saleItem] setGeolati:currentValue];
    }

    else if ([elementName isEqualToString:@"geo:lon"])
    {
        NSString* aString5 = [NSString stringWithFormat: @"%@", currentValue];

        aString5 = [aString5 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString5 length]!=0)
        {
            [life_line_geo geo_long_init:aString5];
            //[aString5 release];


        }



        //[[self saleItem] setGeolong:currentValue];
    }

    else if ([elementName isEqualToString:@"xal:CountryName"])
    {
        NSString* aString6 = [NSString stringWithFormat: @"%@", currentValue];

        aString6 = [aString6 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString6 length]!=0)
        {
            [life_line_address country_name:aString6];
            //[aString6 release];


        }


        //[[self saleItem] setCountryname:currentValue];
    }

    else    if ([elementName isEqualToString:@"xal:LocalityName"]) 
    {
        NSString* aString7 = [NSString stringWithFormat: @"%@", currentValue];

        aString7 = [aString7 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString7 length]!=0)
        {
            [life_line_address locality_name:aString7];
            //[aString7 release];           
        }


        //[[self saleItem] setLocalityname:currentValue];
    }

    else if ([elementName isEqualToString:@"xal:DependentLocalityName"]) 
    {
        NSString* aString8 = [NSString stringWithFormat: @"%@", currentValue];

        aString8 = [aString8 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString8 length]!=0)
        {   [life_line_address dependentlocality_name:aString8];
            //[aString8 release];           
        }


        //[[self saleItem] setDependentlocalityname:currentValue];
    }   

    else if ([elementName isEqualToString:@"xal:ThoroughfareName"]) 
    {
        NSString* aString9 = [NSString stringWithFormat: @"%@", currentValue];

        aString9 = [aString9 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([aString9 length]!=0)
        {
            [life_line_address thoroughfare_name:aString9];
            //[aString9 release];

        }
        //[[self saleItem] setThoughfarename:currentValue];
    }


    else if ([elementName isEqualToString:@"xal:AddressLine"]) 
    {
        NSString* aString10 = [NSString stringWithFormat: @"%@", currentValue];

        aString10 = [aString10 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([aString10 length]!=0)
        {
            [life_line_address postal_code:aString10];
            //[aString10 release];          
        }


        //[[self saleItem] setAddressline:currentValue];
    }

    else if ([elementName isEqualToString:@"poi:phone-country-code"]) 
    {
        NSString* aString11 = [NSString stringWithFormat: @"%@", currentValue];

        aString11 = [aString11 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString11 length]!=0)
        {
            [life_line_phone county_code:aString11];
            //[aString11 release];          
        }
    }
    else if ([elementName isEqualToString:@"poi:phone-area-code"]) 
    {
        NSString* aString12 = [NSString stringWithFormat: @"%@", currentValue];

        aString12 = [aString12 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
        if ([aString12 length]!=0)
        {
            [life_line_phone area_code:aString12];
            //[aString12 release];

        }



        //[[self saleItem] setAreacode:currentValue];
    }
    else if ([elementName isEqualToString:@"poi:phone-subscriber"]) 
    {
        NSString* aString13 = [NSString stringWithFormat: @"%@", currentValue];

        aString13 = [aString13 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([aString13 length]!=0)
        {
            [life_line_phone phone_no:aString13];
            //[aString13 release];

        }


        //[[self saleItem] setPhoneno:currentValue];
    }

    else if ([elementName isEqualToString:@"poi:email"]) 
    {
        NSString* aString14 = [NSString stringWithFormat: @"%@", currentValue];

        aString14 = [aString14 stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];

        if ([aString14 length]!=0)
        {
            [life_line_email lifeline_email:aString14];
            //[aString14 release];  
        }



        //[[self saleItem] setEmail_s:currentValue];
    }
    else if ([elementName isEqualToString:@"poi:contact"]) 
    {

        NSInteger lifelineinsertrowid; 
        sqlite3_stmt *statement2;
        sqlite3_stmt *statement3;
        sqlite3_stmt *statement4;
        sqlite3_stmt *statement5;
    //  NSLog(@"%@",life_line_detail.lifeline_identifier);
        if (sqlite3_open([path UTF8String], &amp; database) == SQLITE_OK)
        { 

                NSString *insertSQL = [NSString stringWithFormat: @"INSERT INTO lifeline (pn_lifeline_identifier,lifeline_contact_name,lifeline_latitude,lifeline_longitute,lifeline_images,distance) Values (\"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%f\")",life_line_detail.lifeline_identifier ,life_line_detail.lifeline_title,life_line_geo.geo_latitude,life_line_geo.geo_longitude ,life_line_detail.lifeline_image,0.00];

            if (sqlite3_prepare_v2(database, [insertSQL UTF8String], -1, &amp;statement2, NULL) == SQLITE_OK)
            {

                if(SQLITE_DONE != sqlite3_step(statement2))
                    NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
                else
                {

                    //SQLite provides a method to get the last primary key inserted by using sqlite3_last_insert_rowid
                    lifelineinsertrowid = sqlite3_last_insert_rowid(database);
                    if (lifelineinsertrowid&gt;0)
                    {
                        NSString *insertSQL1 = [NSString stringWithFormat: @"INSERT INTO lifeline_contact (lifeline_id ,address_type,lifeline_country,lifeline_locality,lifeline_dependentlocality,lifeline_throughfare,lifeline_pastalcode) Values (\"%d\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\", \"%@\")", lifelineinsertrowid,life_line_address.address_type ,life_line_address.country_name ,life_line_address.locality_name,life_line_address.dependentlocality_name ,life_line_address.thoroughfare_name ,life_line_address.postal_code];


                        if (sqlite3_prepare_v2(database, [insertSQL1 UTF8String], -1, &amp;statement3, NULL) == SQLITE_OK)
                        {
                            if(SQLITE_DONE != sqlite3_step(statement3))
                                NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
                            else
                            {

                                NSString *insertSQL2 = [NSString stringWithFormat: @"INSERT INTO lifeline_emails (lifeline_id ,lifeline_emailtype,lifeline_email) Values (\"%d\",  \"%@\", \"%@\")", lifelineinsertrowid,life_line_email.email_type ,life_line_email.lifeline_email];


                                if (sqlite3_prepare_v2(database, [insertSQL2 UTF8String], -1, &amp;statement4, NULL) == SQLITE_OK)
                                {
                                    if(SQLITE_DONE != sqlite3_step(statement4))
                                        NSAssert1(0, @"Error while inserting data. '%s'", sqlite3_errmsg(database));
                                    else
                                    {
                                        NSString *insertSQL3 = [NSString stringWithFormat: @"INSERT INTO lifeline_phones (lifeline_id ,country_code,area_code,lifeline_phoneno,lifeline_phonetype) Values (\"%d\",  \"%@\", \"%@\",\"%@\",\"%@\")", lifelineinsertrowid,life_line_phone.county_code,life_line_phone.area_code,life_line_phone.phone_no,life_line_phone.phone_type];


                                        if (sqlite3_prepare_v2(database, [insertSQL3 UTF8String], -1, &amp;statement5, NULL) == SQLITE_OK)
                                        {
                                            if(SQLITE_DONE == sqlite3_step(statement5))
                                            {
                                                //NSLog(@"Insert all data");
                                            }
                                            else
                                            {
                                                NSLog(@"Error to insert");
                                            }

                                        }
                                        sqlite3_reset(statement5);
                                    }
                                }
                                sqlite3_reset(statement4);
                            }
                        } 
                        sqlite3_reset(statement3);


                    }
                }
            }sqlite3_reset(statement2);
            sqlite3_close(database);            
        }               



    else
    {

        sqlite3_close(database);
        NSAssert1(0, @"Failed to open database: '%s'.",sqlite3_errmsg(database));
    }

    }
    [currentValue release];
    currentValue=nil;

}
    - (void)parserDidEndDocument:(NSXMLParser *)parser
{

    NSLog(@"%x",[life_line_email retainCount]);
    parse_finished=YES;
}


- (BOOL) lifeLineParsing
{


   NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask,YES);
    xmlPath= [[paths objectAtIndex:0] stringByAppendingPathComponent:@"/uncompressddatafolder/lifelines.xml"];
    NSData *xml = [NSData dataWithContentsOfFile:xmlPath];
    parser = [[NSXMLParser alloc] initWithData:xml] ;
    [parser setDelegate:self];
    [parser setShouldProcessNamespaces:NO]; // We don't care about namespaces
    [parser setShouldReportNamespacePrefixes:NO]; //
    [parser setShouldResolveExternalEntities:NO];
    [parser parse];
    [parser setDelegate:nil];
    [parser release];

    return parse_finished;
}
    - (void) dealloc
    {   
        if(life_line_address)
        {
            [life_line_address release];
            life_line_address=nil;

        }
        if(life_line_geo)
        {[life_line_geo release];
            life_line_geo=nil;

        }
        if (life_line_phone) {
            [life_line_phone release];
            life_line_phone=nil;

        }
        if (life_line_detail) {
            [life_line_detail release];
            life_line_detail=nil;
        }
        if (life_line_email) {
            [life_line_email release];
            life_line_email=nil;
        }       
        if (mydatabasepath) {
            [mydatabasepath release];
            mydatabasepath=nil;
        }



        [super dealloc];
    }


@end
</code></pre>

<p>when i adopt this type concept is i am right or wrong. </p>

<p>Because my application crass With this Error </p>

<p>-[CFString release]: message sent to deallocated instance 0x11815530</p>

<p>I really could not find out where i am wrong. </p>

<p>Please Help Me. </p>

<p>I am Stuck in this problem last 20 Days. </p>

<p>and Please Please Help me </p>
