Objective-C memory management problem -
i'm getting exc_bad_access error, , it's because of part of code. basically, take input , work on it. after multiple inputs, throws error. doing wrong memory here? i'd post rest of code, it's rather long -- , think may problem lies (it's xcode points me, @ least).
-(ibaction) findshows: (id) clicked { char urlchars[1000]; [self geteventurl: urlchars]; nsstring * theurl = [[nsstring alloc] initwithformat:@"%s", urlchars]; nsdata *data = [nsdata datawithcontentsofurl:[nsurl urlwithstring:theurl]]; int thelength = [data length]; nsstring *content = [nsstring stringwithutf8string:[data bytes]]; char eventdata[[data length]]; strcpy(eventdata, [content utf8string]); [self parseeventdata: eventdata datalength: thelength]; [whatisshowing setstringvalue:@"showing events artist"]; }
when crash occurs, there backtrace.
post it.
either program break in debugger, , call stack in debugger ui (or can type 'bt
with that, cause of crash quite obvious. without that, left critique code.
so, here goes....
char urlchars[1000]; [self geteventurl: urlchars];
this is, @ best, security hole and, @ worst, source of crash. time going copy bytes buffer, there should kind of way (a) limit # of bytes copied in (pass length of buffer) , (b) # of bytes copied returned (0 failure or no bytes copied).
given above, happens if there 1042 bytes copied urlchars
geteventurl:
? boom
nsstring * theurl = [[nsstring alloc] initwithformat:@"%s", urlchars];
this making assumptions urlchars
lead failure. first, assumes urlchars
of proper %s
compatible encoding. secondly, assumes urlchars
null terminated (and didn't overflow buffer).
best use 1 of various nsstring methods create strings directly buffer of bytes using particular encoding. more precise , more efficient.
nsdata *data = [nsdata datawithcontentsofurl:[nsurl urlwithstring:theurl]];
i hope isn't on main thread... 'cause it'll block if , that'll make app unresponsive on slow/flaky networks.
int thelength = [data length]; nsstring *content = [nsstring stringwithutf8string:[data bytes]]; char eventdata[[data length]]; strcpy(eventdata, [content utf8string]);
this least efficient possible way of doing this. there no need create nsstring
instance turn (char *)
. grab bytes
data directly.
also -- sure data returned null terminated? if not, strcpy()
gonna blow right past end of eventdata
buffer, corrupting stack.
[self parseeventdata: eventdata datalength: thelength]; [whatisshowing setstringvalue:@"showing events artist"];
what kind of data parsing want parse raw bytes? in cases, such data should of kind of structured type; xml or, even, html. if so, there no need drop down parsing raw bytes. (not raw data unheard of -- odd).
Comments
Post a Comment