[Mapserver-dev] Issue with undefining string attributes using mapscript

Steve Lime steve.lime at dnr.state.mn.us
Tue Mar 11 15:09:12 EST 2003


See below for my exchange with the Swig folks...

Steve

Steve Lime writes:
 > Thanks Dave!
 > 
 > >>> David Beazley <beazley at cs.uchicago.edu> 12/09/02 10:44 AM >>>
 > Steve Lime writes:
 >  > Um, yup it is. Simply switching:
 >  > 
 >  > if (arg1->name) free((char*)arg1->name);
 >  > arg1->name = (char *) malloc(strlen(arg2)+1);
 >  > strcpy((char*)arg1->name,arg2);
 >  > 
 >  > to:
 >  > 
 >  > if (arg1->name) free((char*)arg1->name);
 >  > if(arg2) { 
 >  >   arg1->name = (char *) malloc(strlen(arg2)+1);
 >  >   strcpy((char*)arg1->name,arg2);
 >  > } else
 >  >   arg1->name = NULL;
 >  > 
 >  > Seems to fix things. Now, how to get these changes made?
 >  > 
 > 
 > This is probably being controlled by a typemap in Lib/swig.swg.
 > I'll take a look.
 > 

You can try sticking this in your interface (or making mods in
swig.swg).

Cheers,

Dave

/* Default typemap for handling char * members */

#ifdef __cplusplus
%typemap(memberin) char * {
  if ($1) delete [] $1;
  if ($input) {
     $1 = ($1_type) (new char[strlen($input)+1]);
     strcpy((char *) $1,$input);
  } else {
     $1 = 0;
  }
}
%typemap(memberin,warning="451:Setting const char * member may leak
memory.") const char * {
  if ($input) {
     $1 = ($1_type) (new char[strlen($input)+1]);
     strcpy((char *) $1,$input);
  } else {
     $1 = 0;
  }
}
%typemap(globalin) char * {
  if ($1) delete [] $1;
  if ($input) {
     $1 = ($1_type) (new char[strlen($input)+1]);
     strcpy((char *) $1,$input);
  } else {
     $1 = 0;
  }
}
%typemap(globalin,warning="451:Setting const char * variable may leak
memory.") const char * {
  if ($input) {
     $1 = ($1_type) (new char[strlen($input)+1]);
     strcpy((char *) $1,$input);
  } else {
     $1 = 0;
  }
}
#else
%typemap(memberin) char * {
  if ($1) free((char*)$1);
  if ($input) {
     $1 = ($1_type) malloc(strlen($input)+1);
     strcpy((char*)$1,$input);
  } else {
     $1 = 0;
  }
}
%typemap(memberin,warning="451:Setting const char * member may leak
memory.") const char * {
  if ($input) {
     $1 = ($1_type) malloc(strlen($input)+1);
     strcpy((char*)$1,$input);
  } else {
     $1 = 0;
  }
}
%typemap(globalin) char * {
  if ($1) free((char*)$1);
  if ($input) {
     $1 = ($1_type) malloc(strlen($input)+1);
     strcpy((char*)$1,$input);
  } else {
     $1 = 0;
  }
}
%typemap(globalin,warning="451:Setting const char * variable may leak
memory.") const char * {
  if ($input) {
     $1 = ($1_type) malloc(strlen($input)+1);
     strcpy((char*)$1,$input);
  } else {
     $1 = 0;
  }
}




More information about the mapserver-dev mailing list