Bad Exception after using a C++ class in IOS -


i'm trying add fuzzy search ios app have built using existing c++ implementation of double_metaphone maurice aubrey http://aspell.net/metaphone/. i've renamed main class double_metaphone.mm, set file type c++ source , got project build.

however getting exc_bad_access error after call doublemetaphone method. of app uses arc , see number of memory management things in double_metaphone.mm class. said suspicious of how i'm trying answer method. here relevant part of double_metaphone.h (i enclose method declaration in extern "c" per calling c++ method objective c).

void doublemetaphone(const char *str,             char **codes); 

here relavant parts of method implementation in double_metaphone.mm

void doublemetaphone(const char *str, char **codes) { int        length; metastring *original; metastring *primary; metastring *secondary; int        current; int        last;  current = 0; /* need real length , last prior padding */ length  = strlen(str);  last    = length - 1;  original = newmetastring(str); /* pad original can index beyond end */ metaphadd(original, "     ");  primary = newmetastring(""); secondary = newmetastring(""); primary->free_string_on_destroy = 0; secondary->free_string_on_destroy = 0;  makeupper(original);  /* skip these when @ start of word */ if (stringat(original, 0, 2, "gn", "kn", "pn", "wr", "ps", "")) current += 1;  /* initial 'x' pronounced 'z' e.g. 'xavier' */ if (getat(original, 0) == 'x')   {   metaphadd(primary, "s");  /* 'z' maps 's' */   metaphadd(secondary, "s");   current += 1;   }  /* main loop */ while ((primary->length < 4) || (secondary->length < 4))     {   if (current >= length)       break;    switch (getat(original, current))  ..... 

a whole bunch of cases each possible characters if statements pick out different fuzzy codings , adding letters primary , secondary identified.

.....  if (primary->length > 4) setat(primary, 4, '\0');  if (secondary->length > 4) setat(secondary, 4, '\0');  *codes = primary->str; *++codes = secondary->str;  destroymetastring(original); destroymetastring(primary); destroymetastring(secondary); } 

finally here snippet of code calls method. import double_metaphone.h , in method following:

- (uitableviewcell *)tableview:(uitableview *)tableview cellforrowatindexpath:(nsindexpath *)indexpath { static nsstring *cellidentifier = @"filtered word"; uitableviewcell *cell = [tableview dequeuereusablecellwithidentifier:cellidentifier]; if (cell == nil) {     cell = [[uitableviewcell alloc] initwithstyle:uitableviewcellstyledefault reuseidentifier:cellidentifier]; }     word *word = [self.fetchedresultscontroller objectatindexpath:indexpath];      char *codes;     nsstring *wordformetaphone = [nsstring stringwithstring:word.spelling];     doublemetaphone([wordformetaphone utf8string], &codes);     nslog(@"doublemetaphone code = %s", codes);      cell.textlabel.text = word.spelling;  return cell; } 

the log messages seems execute no issue, cell.textlable.text = word.spelling triggers exc_bad_access error. when step through code can't po word.spelling either - i'm thinking c++ code blowing pointers/memory. don't know start fix it.

notes: passed in [word.spelling utf*string] encountered same memory issue. word.spelling == @"a" first time through code. when comment out 4 lines of code app runs fine.

i'm not confident i'm setting char *codes , referencing correctly i'm thinking might contributing issue - shouldn't array of primary , secondary?

i did change definition of doublemetaphone method from

 doublemetaphone(char *str, char **codes)  

because of "deprecated conversion string constant 'char*' " warning see c++ deprecated conversion string constant 'char*'. there couple of other methods in c++ class had switch const char* i'm hoping isn't part of problem?

please help!

your problem is

    *++codes = secondary->str; 

you're passing pointer one char* function, ++codes invalid pointer (you're overwriting crucial happens stored next codes in calling function).

if want return 2 things, can either pass 2 char** parameters - 1 "primary" , 1 "secondary" - or can pass pointer array, this:

char* outputs[2] = {0, 0}; doublemetaphone([wordformetaphone utf8string], outputs); nslog(@"doublemetaphone primary = %s, secondary = %s", outputs[0], outputs[1]);  ... // in doublemetaphone: codes[0] = primary->str; codes[1] = secondary->str; 

Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -