[Gdal-dev] Calling OGRLayer member functions from MFC::OnDraw

Mateusz Loskot mateusz at loskot.net
Tue Sep 19 14:57:28 EDT 2006


Ricardo wrote:
> Hi,
> 
> I'm trying to call OGRLayer member functions from MFC::OnDraw and it was 
> returning to me an Access violation reading location.
> 
> [...]
> 
> That is my source:
> ///////////////////////////////////////////////////////////////////////////
> 
> CChildView::CChildView()
> {

Hmm, it's not the OnDraw() handler, but a class constructor.

> const char *conexao = "PG:user=postgres password=postgres dbname=testegis 
> host=10.0.0.62 port=5432";
> 
> OGRRegisterAll();
> CPLPushErrorHandler( CPLQuietErrorHandler );
> 
> CPLSetConfigOption("GML_FIELDTYPES","ALWAYS_STRING");
> 
>    
>  char szPath[100] = "";
> 
> 
> char *pszDSName = NULL;
> pszDSName = CPLStrdup(conexao);
> char **papszTokens = NULL;
> papszTokens = CSLTokenizeStringComplex( conexao, ",", TRUE, FALSE );
> if( CSLCount(papszTokens) > 0 )
>       pszDSName = CPLStrdup( papszTokens[0] );
> CSLDestroy(papszTokens);
>  if( pszDSName == NULL )
>   {
>       exit( 1 );
>   }


It's really not a good idea to run exit from constructors.
The only option you have is to throw an exception from constructor.


> #ifdef USE_SHARED_ACCESS
>      poDS = OGRSFDriverRegistrar::GetRegistrar()->OpenShared( pszDSName );
> #else
>       poDS = OGRSFDriverRegistrar::Open( pszDSName );
> #endif
> 
> 
> poOGRLayer = poDS->GetLayer(13); //until here it works well.
> 


Have you checked poOGRLayer for NULL here?
Please, try to add some assertion below GetLayer() call.

> poOGRLayer->ResetReading(); // it causes a  Access violation reading location


Cheers
-- 
Mateusz Loskot
http://mateusz.loskot.net



More information about the Gdal-dev mailing list