[Mapserver-dev] file handle leakage in mapswf.c?

Steve Spicklemire steve at spvi.com
Wed Jul 16 09:44:22 EDT 2003


Hi Mapserver developers..

First, I want to thank you for a fantastic product. In a very short 
period of time, I've moved the mapping code of a complex system to 
mapscript. We're developing a sort of property/inventory management 
system with Zope and flash. We needed maps to make it possible to view 
the system in a graphical way. I started with ming alone, but happily 
someone pointed out Mapserver, and I found that with Mapserver, and 
PostGIS I could do everything I needed to. The only problem was that 
scaling, rotating, and sometimes even presentation of fonts wasn't 
working. I scoured the mailing lists.. and fiddled around with 
variations on mapfiles for several days. Yesterday, I decided that I 
needed to dig in to the code with the debugger and find out what was 
really happening. It seems that mapswf.c allocates a file handle to 
read a font file every time it draws text! I had a *lot* of labels on 
my maps, and my process was running out of file handles. Unfortunately 
this was silently ignored, and the result was simply no output! (By the 
time the code got around to trying to *write* anything, all the file 
handles were consumed by attempts to load the font from the filesystem).

Anyway.. my quick-fix is to simply close the font file every time it's 
read. Probably some sort of font-cache would make more sense. (I think 
that ming may already *have* a font cache.. I'll look into what it 
would take to find out.)

Here are my patches to mapswf.c...

-steve

diff -C3 -r1.29 mapswf.c
*** mapswf.c    11 Jun 2003 17:54:42 -0000      1.29
--- mapswf.c    16 Jul 2003 13:31:12 -0000
***************
*** 1563,1580 ****
       if (!string || !pszFontFile || !psColor)
           return NULL;

!     oFont  = loadSWFFontFromFile(fopen(pszFontFile, "rb"));
!     if (oFont)
!     {
!         oText = newSWFText();
!         SWFText_setFont(oText, oFont);
!         SWFText_moveTo(oText, (float)nX, (float)nY);
!         SWFText_setColor(oText, (byte)psColor->red, 
(byte)psColor->green, (byte)psColor->blue,
!                          0xff);
!         SWFText_setHeight(oText, (float)dfSize);
!         SWFText_addString(oText, string, NULL);

!         return oText;
       }

       return NULL;
--- 1563,1585 ----
       if (!string || !pszFontFile || !psColor)
           return NULL;

!     FILE *f = fopen(pszFontFile, "rb");
!
!     if (f) {
!       oFont  = loadSWFFontFromFile(f);
!       if (oFont)
!       {
!         fclose(f);
!         oText = newSWFText();
!         SWFText_setFont(oText, oFont);
!         SWFText_moveTo(oText, (float)nX, (float)nY);
!         SWFText_setColor(oText, (byte)psColor->red, 
(byte)psColor->green, (byte)psColor->blue,
!                          0xff);
!         SWFText_setHeight(oText, (float)dfSize);
!         SWFText_addString(oText, string, NULL);

!         return oText;
!       }
       }

       return NULL;
***************
*** 1896,1902 ****

   //    char *error=NULL, *font=NULL;
   //    int bbox[8];
- //    double angle_radians = MS_DEG_TO_RAD*label->angle;

       size = label->size*scalefactor;

--- 1901,1906 ----
***************
*** 1953,1963 ****
         return(-1);
       }

!     oText = DrawText(string, x, y, msBuildPath(szPath, 
fontset->filename, font), size, &sColor);
       if (oText)
       {
           //nTmp = image->img.swf->nCurrentMovie;
!         SWFMovie_add(GetCurrentMovie(map, image), oText);
       }

       return 0;
--- 1957,1970 ----
         return(-1);
       }

!     oText = DrawText(string, 0, 0, msBuildPath(szPath, 
fontset->filename, font), size, &sColor);
       if (oText)
       {
+         SWFDisplayItem i;
           //nTmp = image->img.swf->nCurrentMovie;
!         i = SWFMovie_add(GetCurrentMovie(map, image), oText);
!         SWFDisplayItem_moveTo(i, x, y);
!       SWFDisplayItem_rotate(i, (float)label->angle);
       }

       return 0;
***************
*** 1997,2005 ****
           return(-1);
       }

!     oFont  = loadSWFFontFromFile(fopen(msBuildPath(szPath, 
fontset->filename, font), "rb"));
       if (oFont)
       {
           oText = newSWFText();
           SWFText_setFont(oText, oFont);
           //SWFText_addString(oText, string, NULL);
--- 2004,2014 ----
           return(-1);
       }

!     FILE *f = fopen(msBuildPath(szPath, fontset->filename, font), 
"rb");
!     oFont  = loadSWFFontFromFile(f);
       if (oFont)
       {
+         fclose(f);
           oText = newSWFText();
           SWFText_setFont(oText, oFont);
           //SWFText_addString(oText, string, NULL);
***************
*** 2543,2548 ****
--- 2552,2558 ----
       int         nLength;
       int         iPointPos;
       int         iSlashPos;
+     int         iSaveResult;
       char        szAction[200];
       SWFAction   oAction;
       mapObj      *map = NULL;
***************
*** 2558,2565 ****
                                                  "OUTPUT_MOVIE",""),
                          "MULTIPLE") != 0)
           {
!             SWFMovie_save(image->img.swf->sMainMovie, filename);
               return(MS_SUCCESS);
           }

   /* 
-------------------------------------------------------------------- */
--- 2568,2579 ----
                                                  "OUTPUT_MOVIE",""),
                          "MULTIPLE") != 0)
           {
!         iSaveResult = SWFMovie_save(image->img.swf->sMainMovie, 
filename);
!         if (!iSaveResult) {
               return(MS_SUCCESS);
+         }else{
+             return(MS_FAILURE);
+         }
           }




More information about the mapserver-dev mailing list