[Gdal-dev] Null-Pointer in GDALOpen?
Mateusz Loskot
mateusz at loskot.net
Mon Oct 22 17:02:27 EDT 2007
Tom Kazimiers wrote:
> Mateusz Loskot schrieb:
>> Tom Kazimiers wrote:
>>> thanks for the replys.
>>> Sure I do test against null - the posted code was just an excerpt :) -
>>> thanks anyway.
>>> And yes, the crash occurs within GDALOpen() - I put a MessageBox before
>>> and after GDALOpen() - only the first one appeared, than the crash.
>>> The file handle explanation looked promising but before this line is
>>> arrived I still can open files plus after a restart it also does not work.
>> Tom,
>>
>> It sounds strange. Perhaps you could post complete snippet of code,
>> then it will be easier to investigate it.
>
> Ok, here is a bigger part of it:
>
> bool CGDALContainer::AddFile(const wchar_t * w_pszFileName)
> {
>
> // Convert wchar_t to char
> const int newsize = wcslen(w_pszFileName);
> char *pszFileName = (char *)malloc(newsize + 1);
> int count = wcstombs(pszFileName, w_pszFileName, newsize);
Tom,
wcstombs will work only for ASCII subset of characters.
If w_pszFileName consists of non-ASCII characters, but some characters
from polish, french, czech languages, it will not translate the string
properly. You need to use codepage aware function, like
WideCharToMultiByte from Windows API.
> if (count == 0 || count != newsize) {
> FILE * pFile = _wfopen(L"PDAShpTool_ErrorLog.log",L"a");
> fwprintf(pFile, L"Error: The Filename \"%s\" could not be
> converted to Const Char *!\n", w_pszFileName);
> fwprintf(pFile, L" Charactos before: %d Converted items:
> %d\n", newsize, count);
> fclose(pFile);
> return false;
> }
> pszFileName[newsize] = '\0'; // one never knows...
> GDALDataset *poDataset = NULL;
>
> GDALAllRegister();
>
> MessageBoxW(NULL, L"HA", L"DLL", MB_OK);
> poDataset = (GDALDataset *) GDALOpen( "\a.tif" /*pszFileName*/,
> GA_ReadOnly );
Replace \a.tif with \\a.tif
>>> The path "a.tif" is the full path of the file as this image is stored in
>>> root directory of Windows CE 5.
>> Do you mean that using file name only works like full path to root
>> directory \, then it's wrong. It doesn't work this way under Windows CE.
>>
>> If you use only file name, it's assumed you open it from \My Documents,
>> which is a default data location under Windows CE.
>
> Thanks for this hint - strange enough that it worked until yesterday
> evening with only the filename for the root directory.
> One more thing is that if I put "a.tif" in to open it crashes - if I put
> "\a.tif" in it does not crash - it just returns NULL and I
> can handle this (besides that the null return is strange, also). The
> error returned by CPLGetLastError() is:
> CPLError: `.tif' does not exist in the file system
yes, because \a is escape sequence but not backslach + 'a' letter.
Use \\a to get string like "\a"
> This error was without convertig from wchar_t to char - it was with
> "\a.tif" inserted directly. It seems as if the const char * is not
> received properly.
Because you don't get what you expected as filename.
> Like I said, I can not really imagine that the problem is really
> GDALOpen as it worked until yesterday and I did not change the dll.
> But the crash occurs there.
Perhaps you accidentally removed slash from correct sequence of \\
>>> The last thing I tryed was to put "GDALAllRegister();" right befor
>>> GDALOpen() - before this I did it in completely different function,
>>> which I think, should work, too.
>>> But also this change did it not...
>> Another thing I'd suggest to check is to see if you have built GDAL with
>> GTiff driver, means check if the GTiff driver is properly registered
>> and available, in run-time.
>
> At least returns
> "GetGDALDriverManager()->GetDriverByName("GTiff")->GetDescription()":
> GTiff
>
> So I think it gets registered properly.
Yes, it's OK.
Cheers
--
Mateusz Loskot
http://mateusz.loskot.net
More information about the Gdal-dev
mailing list