[gdal-dev] MEM Datasets

James Meyer jamesm at lantic.net
Tue Jul 13 08:29:23 EDT 2010


Hi

Actually I did call it.
Sorry I just didn't include it in the code because it was much earlier 
in the program.

However after some playing around, it seems that the error only occurs 
IF I did a write to the MEM dataset. (Although it occurs WHEN I close it).

The code I am using to test is below line-for-line exact. I basically 
read from an existing dataset (known to be GDT_Byte) into the in-memory 
dataset.

It seems like some sort of buffer overflow, because if I delete the last 
two lines (that frees the buffers) I get a long "stack smash detected" 
printout in stead of a segmenation fault.

Can you see anything I am doing wrong?

Thanks

//************************************************
GDALAllRegister();
  char filename[255];
int sizey = 100;
int sizex = 100;
  int datasize = sizex * sizey * (GDALGetDataTypeSize(GDT_Byte) / 8);

  void* data = malloc(datasize);
  sprintf(filename,
    "MEM:::DATAPOINTER=%p,PIXELS=%d,LINES=%d,BANDS=%d,DATATYPE=%s",
&data, 100, 100, 1, GDALGetDataTypeName(GDT_Byte));

  GDALDatasetH dst = GDALOpen(filename,GA_Update);
  GDALDataset* dest = (GDALDataset*) dst;
  GDALRasterBand * destBand = dest->GetRasterBand(1);

GDALDataset* source = (GDALDataset*) GDALOpen("images/ref.pix", 
GA_ReadOnly);
GDALRasterBand* sourceBand = source->GetRasterBand(1);

  if (! dest || !source )
  {
     printf("Failed to open Dataset");
  }

void* buffer = malloc(sizex*sizey); //For byte data type

   if (CE_Failure == sourceBand->RasterIO(GF_Read, 0, 0,sizex, sizey, 
buffer, sizex, sizey, GDT_Byte ,0,0))
     printf("READ FAILED");

   if (CE_Failure ==destBand->RasterIO(GF_Write, 0, 0,sizex, sizey, 
buffer, sizex, sizey, GDT_Byte,0,0 ))
    printf("WRITE FAILED");

printf("Releasing Resources\n");

  GDALClose(dst);
  free(data);
free(buffer);

//**************************************************
On 13/07/2010 13:37, Chaitanya kumar CH wrote:
> James,
>
> You forgot to call GDALAllRegister() before opening the dataset. Then 
> you will not get a NULL with GDALOpen().
> http://www.gdal.org/gdal_tutorial.html
>
> On Tue, Jul 13, 2010 at 3:40 PM, James Meyer <jamesm at lantic.net 
> <mailto:jamesm at lantic.net>> wrote:
>
>     Hi
>
>     I am trying to use MEM (In-memory) datasets with GDAL.
>     However, on trying to close the dataset I get a segmentation fault.
>     The dataset is created successfully. The handle returned is valid
>     and I am able to read and write to it.
>
>     Am I not supposed to try and close the handle or am I doing
>     something else wrong?
>
>     The code is as follows:
>
>     //************************************************
>      char filename[255];
>      int datasize = 100 * 100 * (GDALGetDataTypeSize(GDT_Byte) / 8);
>
>      void* data = malloc(datasize);
>      sprintf(filename,
>        "MEM:::DATAPOINTER=%p,PIXELS=%d,LINES=%d,BANDS=%d,DATATYPE=%s",
>     &data, 100, 100, 1, GDALGetDataTypeName(GDT_Byte));
>
>
>      GDALDatasetH dst = GDALOpen(filename,GA_Update);
>      GDALDataset* dest = (GDALDataset*) dst;
>
>      if (! dest )
>      {
>         printf("Failed to open Dataset");
>      }
>      GDALClose(dst); //This fails
>      free(data);
>     //**************************************************
>
>     Thanks
>
>     James
>     _______________________________________________
>     gdal-dev mailing list
>     gdal-dev at lists.osgeo.org <mailto:gdal-dev at lists.osgeo.org>
>     http://lists.osgeo.org/mailman/listinfo/gdal-dev
>
>
>
>
> -- 
> Best regards,
> Chaitanya kumar CH.
> /tʃaɪθənjə/ /kʊmɑr/
> +91-9494447584
> 17.2416N 80.1426E


More information about the gdal-dev mailing list