objective c - Resolving EXC_BAD_ACCESS Issue In Cocoa? -



hey..i have following method in cocoa..

-(void)startuploadwithcontainername:(nsstring *)containername { //make object of nsfilemanager , fetch array of local folder contents , cloud folder contents nsfilemanager *uploadmanager=[[nsfilemanager alloc] init]; nsstring *uploadpath=[[[nsstring alloc] initwithstring:@"~/cloud briefcase"] stringbyexpandingtildeinpath]; nserror *err; nsarray *uploadfoldercontents=[uploadmanager contentsofdirectoryatpath:uploadpath error:&err]; asicloudfilesobjectrequest *cloudlist = [asicloudfilesobjectrequest listrequestwithcontainer:containername]; [cloudlist startsynchronous]; nsarray *cloudfoldercontents = [cloudlist objects];  [cloudlist release]; [uploadmanager release];  nslog(@"%lu",[uploadfoldercontents count]); nslog(@"\n%@\n\n%@",cloudfoldercontents,uploadfoldercontents); nsstring *notfoundpath; nsstring *foundpath; nsstring *foundcloudmatch; nsdate *clouduploaddate;  (int j=1; j<[uploadfoldercontents count]; j++) {     int i=0;     (int k=0; k<[cloudfoldercontents count]; k++) {         if ([[[cloudfoldercontents objectatindex:k] name] isequaltostring:[uploadfoldercontents objectatindex:j]]) {             i=1;             foundpath=[uploadfoldercontents objectatindex:j];             foundcloudmatch=[cloudfoldercontents objectatindex:k];             clouduploaddate=[[cloudfoldercontents objectatindex:k] lastmodified];             break;         }         else{             i=0;             notfoundpath=[uploadfoldercontents objectatindex:j];             continue;         }     }      if (i==1) {         nslog(@"found in cloud: %@",foundpath);         nsstring *uploadpath=[[nsstring stringwithformat:@"~/cloud briefcase/%@",foundpath] stringbyexpandingtildeinpath];         nstimezone *tcst=[nstimezone timezonewithabbreviation:@"cst"];         nsinteger clouddifference=[tcst secondsfromgmtfordate:clouduploaddate];          nsfilemanager *typemanager=[[nsfilemanager alloc] init];         nserror *er;         nsdictionary *propertiesofuploadfile=[typemanager attributesofitematpath:uploadpath error:&er];          nsdate *localuploaddate=[propertiesofuploadfile objectforkey:nsfilemodificationdate];          nsinteger sourceuploaddifference=[[nstimezone systemtimezone] secondsfromgmtfordate:localuploaddate];           nslog(@"local date %@",localuploaddate);         nslog(@"local difference %ld",sourceuploaddifference);         nstimeinterval diff=sourceuploaddifference-clouddifference;         nstimeinterval sdiff=sourceuploaddifference;         nsdate *ldate=[[nsdate alloc] initwithtimeinterval:sdiff sincedate:localuploaddate];         nsdate *comparisondate=[[nsdate alloc] initwithtimeinterval:diff sincedate:clouduploaddate];         nslog(@"\nsdiff value %@",ldate);         nslog(@"comparison date %@",comparisondate);          [localuploaddate release];         [propertiesofuploadfile release];         [typemanager release];         [tcst release];          if ([comparisondate compare:ldate]==nsorderedascending) {             [comparisondate release];             [ldate release];             nslog(@"got it");             nsstring *escstring=[foundpath stringbyaddingpercentescapesusingencoding:nsasciistringencoding];             asicloudfilesobjectrequest *request =              [asicloudfilesobjectrequest putobjectrequestwithcontainer:containername objectpath:escstring contenttype:@"file" file:uploadpath metadata:nil etag:nil];             [request startsynchronous];             nslog(@"uploaded %@",foundpath);         }        }     else{         nslog(@"not found in cloud: %@",notfoundpath);         nsstring *uploadpath=[[nsstring stringwithformat:@"~/cloud briefcase/%@",notfoundpath] stringbyexpandingtildeinpath];         //          nslog(@"%@",uploadpath);           nsstring *escstring=[notfoundpath stringbyaddingpercentescapesusingencoding:nsasciistringencoding];         nslog(@"url encoded value: %@",escstring);          asicloudfilesobjectrequest *request =          [asicloudfilesobjectrequest putobjectrequestwithcontainer:containername objectpath:escstring contenttype:@"file" file:uploadpath metadata:nil etag:nil];         [request startsynchronous];         nslog(@"upload complete");     } } [uploadpath release];  [cloudlist release]; [uploadfoldercontents release];   } 

but hangs showing exception

received signal exc_bad_access

can clear issue out? exception occurs @ nslog(@"found in cloud: %@",foundpath);

typically set environment variable nszombieenabled yes , troubleshoot issue. in case see have declared pointers without pointing object dangerous. running code in clang analyzer report warning. set pointers nil. have declared string pointer in loop if "if" not true goes else foundpath never pointed , tried access in if(i==1)

update: consider lou franco 's answer. correct too. don't own cloudlist object. autoreleased , over-releasing passing release message cloudlist object [cloudlist release]. in case might not crash instantly when release because control in same loop. once current threads autorelease pool drained code crash exc_bad_access.

asicloudfilesobjectrequest *cloudlist = [asicloudfilesobjectrequest listrequestwithcontainer:containername]; [cloudlist startsynchronous]; nsarray *cloudfoldercontents = [cloudlist objects]; [cloudlist release];// remove line  

update2:

nsstring *uploadpath=[[[nsstring alloc] initwithstring:@"~/cloud briefcase"] stringbyexpandingtildeinpath]; 

change above

nsstring *uploadpath=[[nsstring stringwithformat:@"~/cloud briefcase"] stringbyexpandingtildeinpath]; 

uploadpath in above line still points autoreleased object. leaking string have created. calling release wrong. remove [uploadpath release] , [cloudlist release] releasing again , again. , why releasing autoreleased object uploadfoldercontents ? remove these 3 lines code:

[uploadpath release];  [cloudlist release]; [uploadfoldercontents release]; 

update3: fixed over-release issues. , updatepath in if block changed updatepathlocal there conflict updatepath variable scope of method.

-(void)startuploadwithcontainername:(nsstring *)containername { //make object of nsfilemanager , fetch array of local folder contents , cloud folder contents nsfilemanager *uploadmanager=[[nsfilemanager alloc] init]; nsstring *uploadpath=[[nsstring stringwithformat:@"~/cloud briefcase"] stringbyexpandingtildeinpath]; nserror *err = nil; nsarray *uploadfoldercontents=[uploadmanager contentsofdirectoryatpath:uploadpath error:&err]; asicloudfilesobjectrequest *cloudlist = [asicloudfilesobjectrequest listrequestwithcontainer:containername]; [cloudlist startsynchronous]; nsarray *cloudfoldercontents = [cloudlist objects];  [uploadmanager release];  nslog(@"%lu",[uploadfoldercontents count]); nslog(@"\n%@\n\n%@",cloudfoldercontents,uploadfoldercontents); nsstring *notfoundpath = nil; nsstring *foundpath = nil; nsstring *foundcloudmatch = nil; nsdate *clouduploaddate = nil;  (int j=1; j<[uploadfoldercontents count]; j++) {     int i=0;     (int k=0; k<[cloudfoldercontents count]; k++) {         if ([[[cloudfoldercontents objectatindex:k] name] isequaltostring:[uploadfoldercontents objectatindex:j]]) {             i=1;             foundpath=[uploadfoldercontents objectatindex:j];             foundcloudmatch=[cloudfoldercontents objectatindex:k];             clouduploaddate=[[cloudfoldercontents objectatindex:k] lastmodified];             break;         }         else{             i=0;             notfoundpath=[uploadfoldercontents objectatindex:j];             continue;         }     }      if (i==1) {         nslog(@"found in cloud: %@",foundpath);         nsstring *uploadpathlocal=[[nsstring stringwithformat:@"~/cloud briefcase/%@",foundpath] stringbyexpandingtildeinpath];         nstimezone *tcst=[nstimezone timezonewithabbreviation:@"cst"];         nsinteger clouddifference=[tcst secondsfromgmtfordate:clouduploaddate];          nsfilemanager *typemanager=[[nsfilemanager alloc] init];         nserror *er = nil;         nsdictionary *propertiesofuploadfile=[typemanager attributesofitematpath:uploadpathlocal error:&er];          nsdate *localuploaddate=[propertiesofuploadfile objectforkey:nsfilemodificationdate];          nsinteger sourceuploaddifference=[[nstimezone systemtimezone] secondsfromgmtfordate:localuploaddate];           nslog(@"local date %@",localuploaddate);         nslog(@"local difference %ld",sourceuploaddifference);         nstimeinterval diff=sourceuploaddifference-clouddifference;         nstimeinterval sdiff=sourceuploaddifference;         nsdate *ldate=[[nsdate alloc] initwithtimeinterval:sdiff sincedate:localuploaddate];         nsdate *comparisondate=[[nsdate alloc] initwithtimeinterval:diff sincedate:clouduploaddate];         nslog(@"\nsdiff value %@",ldate);         nslog(@"comparison date %@",comparisondate);          [typemanager release];          if ([comparisondate compare:ldate]==nsorderedascending) {             [comparisondate release];             [ldate release];             nslog(@"got it");             nsstring *escstring=[foundpath stringbyaddingpercentescapesusingencoding:nsasciistringencoding];             asicloudfilesobjectrequest *request =              [asicloudfilesobjectrequest putobjectrequestwithcontainer:containername objectpath:escstring contenttype:@"file" file:uploadpath metadata:nil etag:nil];             [request startsynchronous];             nslog(@"uploaded %@",foundpath);         }     }     else{         nslog(@"not found in cloud: %@",notfoundpath);         nsstring *uploadpathlocal=[[nsstring stringwithformat:@"~/cloud briefcase/%@",notfoundpath] stringbyexpandingtildeinpath];         //          nslog(@"%@",uploadpath);           nsstring *escstring=[notfoundpath stringbyaddingpercentescapesusingencoding:nsasciistringencoding];         nslog(@"url encoded value: %@",escstring);          asicloudfilesobjectrequest *request =          [asicloudfilesobjectrequest putobjectrequestwithcontainer:containername objectpath:escstring contenttype:@"file" file:uploadpathlocal metadata:nil etag:nil];         [request startsynchronous];         nslog(@"upload complete");     }  } } 

Comments

Popular posts from this blog

objective c - Change font of selected text in UITextView -

php - Accessing POST data in Facebook cavas app -

c# - Getting control value when switching a view as part of a multiview -