1

Can someone help me regarding this problem?

It seem the code can't read the data set of my raster map.It will throw this exception (popup to break or continue).

Unhandled exception at 0x000000013F63298A in GdalTest.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

// GdalTest.cpp : Defines the entry point for the console application.
//


#include "cpl_conv.h" // for CPLMalloc()
#include "stdafx.h"
#include "gdal.h"
#include <stdlib.h>
#include "iostream"
#include "gdal_priv.h"


using namespace std;

class GDALMajorObject;
class GDALDataset;
class GDALRasterBand;
class GDALDriver;
class GDALRasterAttributeTable;
class GDALProxyDataset;
class GDALProxyRasterBand;
class GDALAsyncReader;




int main(int argc, char** argv)
{
     GDALAllRegister(); 

    GDALDataset *poDataset;
    char* Raster = new char[256];
    double  adfGeoTransform[6];

    Raster = "@ C:\\Users\\User\\Desktop\\DSI REFERENCE\\Map Data\\Raster\\4257.tiff";

    poDataset = (GDALDataset *) GDALOpen(Raster, GA_ReadOnly )CPL_WARN_UNUSED_RESULT;
    if( poDataset == NULL )
    {
        cout<<"Damn it"<<endl;
    }         

    cout<<( "Driver: %s/%s\n",
        poDataset->GetDriver()->GetDescription(),
        poDataset->GetDriver()->GetMetadataItem( GDAL_DMD_LONGNAME ) );

    cout<<( "Size is %dx%dx%d\n",
        poDataset->GetRasterXSize(), poDataset->GetRasterYSize(),
        poDataset->GetRasterCount() );

    if( poDataset->GetProjectionRef()  != NULL )
    cout<<( "Projection is `%s'\n", poDataset->GetProjectionRef() );

    if( poDataset->GetGeoTransform( adfGeoTransform ) == CE_None )
    {
        cout<<( "Origin = (%.6f,%.6f)\n",
            adfGeoTransform[0], adfGeoTransform[3] );
        cout<<( "Pixel Size = (%.6f,%.6f)\n",
            adfGeoTransform[1], adfGeoTransform[5] );
    }


    // make it 256 bytes for a string length of 255 plus null (\0) terminator



    // your code
    delete[] Raster;// release the memory VERY IMPORTANT

}

EDIT

It show this error when i try run the program.enter image description here

Then I try to debug the code line by line. It got error reading character on string for this code line

char* Raster = new char[256];

when i about to debug another line it will jump to newaop.cpp tab.

// newaop -- operator new[](size_t) REPLACEABLE
#include <new>

void *__CRTDECL operator new[](size_t count) _THROW1(std::bad_alloc)
    {   // try to allocate count bytes for an array
    return (operator new(count));
    }

then i keep debug it will go debug to tab dbgdel.cpp until it stop at dbgheap.c then throw this

memset.asm not found

  • 1
    Raster = "@ C:\\Use.. shouldn't have the @ inside the quote, try Raster = "C:\\Use.. what line is your error on? Your exception is a null reference exception (a pointer is null or 0x0000000000000000, which is protected memory). Try step debugging and see where it breaks. – Michael Stimson Nov 8 '18 at 7:34
  • I already delete the @ symbol.From edit above i don't get thrown any exception anymore. But it give Debug Assertion Failed. It cause error when I try to debug line by line. This because of the code or the program can't access my raster(.tiff) map file? – Mohamad Fathin Nov 9 '18 at 2:22
1

Your error is weird, I'm guessing here. From my working code:

    char* OutPath  = new char[FullPathMax]; // const int FullPathMax = 4096;
    wcstombs(OutPath,argv[2],FullPathMax);  // put the 2nd argument into OutPath

    GDALAllRegister(); // setup GDAL ready to use

    char* MaskName = new char[MaxLength];     // assign memory for the file path
    sprintf(MaskName,"%s\\Mask.img",OutPath); // change to "C:\\Users\\User\\Desktop\\DSI REFERENCE\\Map Data\\Raster\\4257.tiff"

    GDALDataset* MaskDS = (GDALDataset*)GDALOpen(MaskName,GDALAccess::GA_Update);
    if (MaskDS == NULL)
    {
        cout<<"Damn it"<<endl; // I like this error message you used.
        delete[] OutPath;      // free resources
        delete[] MaskName;     // free resources
        return -1;             // exit here on error
    }
    MaskDS->GetGeoTransform(GeoTransform);

As you can see I don't use = to set the string into the char*, which is solely because I want to use an argument so don't already know what the string is, which could be where your problem lies.. I had to read up on it, see https://stackoverflow.com/questions/18963888/c-char-initialization-in-constructor from the answer by Jerry Coffin and http://www.cplusplus.com/forum/beginner/106589/ the equals assignment Raster = "C:\\Users\\User\\Desktop\\DSI REFERENCE\\Map Data\\Raster\\4257.tiff"; is valid, but being depreciated.

In C++ there is a difference between string and char* but also Windows has wchar* which is a pointer to a unicode character array, depending on your codepage your literal may be unicode and not going into your char* properly. Have a shot at wcstombs(Raster,&"C:\\Users\\User\\Desktop\\DSI REFERENCE\\Map Data\\Raster\\4257.tiff",wcsnlen(&"C:\\Users\\User\\Desktop\\DSI REFERENCE\\Map Data\\Raster\\4257.tiff")); using wcstombs to convert multibyte to char array with the multibyte string length wcsnlen and see if that helps. If not post a question on StackOverflow, with a link to this thread, as this appears to be more C++ than GIS.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.