[mapserver] Java VM Crashes using the Mapscript API

Rick Innis rick at INNIS.CA
Thu Jul 28 15:07:59 EDT 2005


Im revisiting this somewhat old thread to contribute a few insights  
I've found while dealing with the same issue, in the hope it saves  
someone else time and effort in the future. The thread "Mapscript  
API: for object manipulation, not object creation?" , from earlier  
this year, also has some info on this topic.

>>
>> Christian reported a crash in layerObj.delete() which I
>> also detected.
>> I think I understood the following:
>>
>> Let's say, we have an initialized  mapObj mO;
>>
>> layerObj lO = new layerObj(mO);
>> // Say lO is the 5th layer now.
>> // Now lO.swigCMemOwn == true and
>> //     lO.swigCPtr points onto the mO.swigCPtr->layers[4]
>>
>> ...
>> // now the mapObj is deleted but the Java-layerObj continues to live.
>> // the C-layer-Object mO.swigCPtr->layers[4] will deleted and  
>> freed also!
>> mO.delete()
>> // From now on O.swigCPtr points into invalid piece of C-memory.
>> ...

I also encountered this a few months back but didn't have time to  
write it up so coherently. One workaround I was given by Sean was to  
use insertLayer() to add the layer, which makes a copy, allowing the  
allocated Java object to be disposed of cleanly. This also requires  
making and inserting styleObj and classObj instances:

                 // initially, layer not associated with any map
                 layerObj myLayer = new layerObj(null);
                 // initialise layer
                 classObj myClass = new classObj(null);
                 styleObj myStyle  = new styleObj(null);
                 // initialize style, then insert into class
                 myClass.insertStyle(styleObj, -1);
                 // insert class into layer
                 myLayer.insertClass(myClass, -1);
                 // insert layer into map - makes copy!
                 int ret = map.insertLayer(annos, -1);
                 // get reference to inserted copy
                 myLayer = map.getLayer(i);


However, when  I started labelling my dynamic layers I found that my  
labels ended up with a black background.  The workaround I found was  
to define an empty layer in my map, including empty style info, and  
use cloneLayer() to make copies as needed.

The layer definition looks like this:

         # Blank generic annotation layer
         LAYER
                 NAME user_layer
                 STATUS OFF
                 TYPE POINT
                 CLASS
                     STYLE
                         COLOR 255 255 255
                     END
                     LABEL
                         COLOR 255 255 255
                     END
                 END
         END

To use it, I grab the layer from the map, clone it, initialize it,  
and insert the clone into the map:

                 layerObj myLayer = map.getLayerByName 
("blank_layer").cloneLayer();
                 // initialise...
                 int ret = map.insertLayer(annos, -1);

The layer's type can be changed on the fly, so one definition  
suffices for all layers.

     --Rick.



More information about the mapserver-users mailing list