[Mapserver-dev] Trouble with pointers to strings in mapserver

Sean Gillies sgillies at frii.com
Fri Feb 7 13:51:54 EST 2003


Hi all,

I have adopted (through Bugzilla) the request for a msCopyMap function
and am having problems with the string properties of the objects.  I'm
looking for some advice on the proper solution within the context of
the existing mapserver code.

The code I'm testing for msCopyMap starts like this:


int msCopyMap(mapObj *dstMap, mapObj *srcMap) {
   ...
   copyStringProperty(dstMap->name, srcMap->name);
   copyProperty(&(dstMap->status), &(srcMap->status), sizeof(int));
   ...
}

void copyProperty(void *dst, void *src, int size) {
   if (src == NULL) dst = NULL;
   else memcpy(dst, src, size);
}

void copyStringProperty(char *dst, char *src) {
   if (src == NULL) dst = NULL;
   else {
     if (dst == NULL) dst = strdup(src);
     else strcpy(dst, src);
   }
}


I am using functions that test for NULL pointers and wrap memcpy
and strcpy.  This code is working as I expect.  Map names and
numeric properties are copied.

Next, I have started to write a msCopyLayer function which looks
like:


int msCopyLayer(layerObj *dstLayer, layerObj *srcLayer) {
   ...
   copyProperty(&(dstLayer->index), &(srcLayer->index), sizeof(int));
   copyProperty(&(dstLayer->map), &(srcLayer->map), sizeof(mapObj *));
   copyStringProperty(dstLayer->classitem, srcLayer->classitem);
   ...
}


and I call this function from msCopyMap like:


int msCopyMap(mapObj *dstMap, mapObj *srcMap) {
   ...
   copyStringProperty(dstMap->name, srcMap->name);
   copyProperty(&(dstMap->status), &(srcMap->status), sizeof(int));
   copyProperty(&(dstMap->height), &(srcMap->height), sizeof(int));
   copyProperty(&(dstMap->width), &(srcMap->width), sizeof(int));

   copyProperty(&(dstMap->numlayers), &(srcMap->numlayers), sizeof(int));

   for (i = 0; i < dstMap->numlayers; i++) {
     initLayer(&(dstMap->layers[i]), dstMap);
     msCopyLayer(&(dstMap->layers[i]), &(srcMap->layers[i]));
   }
   ...
}


The problem I experience is that copyStringProperty() isn't working
within msCopyLayer().  I stepped through the code using gdb and
discovered that when copyStringProperty() is called within 
msCopyLayer(),
the source string passed to copyStringProperty is being nullified.

For example, in msCopyLayer, before this line

   copyStringProperty(dstLayer->classitem, srcLayer->classitem);

is executed, I have srcLayer->classitem = "CO08_D00_".  Inside
copyStringProperty, I have src = 0x0.

I'm stumped, and wishing that I was more of a real programmer and less
of a hacker.  Why does copyStringProperty() work when called in
msCopyMap(), but not in msCopyLayer()?  I have found one solution and 
that
is to not call copyStringProperty() from msCopyLayer, but instead copy 
its
code explicitly into msCopyLayer.  I'd rather not if it wasn't 
necessary.
I've looked through the rest of the MapServer code and the only similiar
code I found is in loadLayer() where we have

   layer->name = getString()

getString is using the global string msyytext, and returning

   return(strdup(msyytext))

but I don't think I should be using a global in msCopyMap.

Grateful for any advice,
Sean




More information about the mapserver-dev mailing list