Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unsafe destroy when the program call OGRCoordinateTransformation::DestoryCT in loop #11607

Open
myBestLove opened this issue Jan 8, 2025 · 1 comment

Comments

@myBestLove
Copy link

What is the bug?

Using OGRCoordinateTransformation::DestoryCT in a loop during which multiple threads are executed can cause segment fault.

when OGRCoordinateTransformation::DestoryCT,OGRProjCT cache will be destroyed.

Steps to reproduce the issue

Environment:
centos 7
c++ 4.8.5

reproduce:

int thread_test1(const char * filename)
{
    for (int r=0; r<100; r++)
    {
        GDALDataset* dataset = (GDALDataset*)GDALOpen(filename, GA_ReadOnly);
        if (dataset == NULL)
        {
            printf("error open %s\n", filename);
            return -1;
        }
      
        GDALRasterBand* band = dataset->GetRasterBand(1);
        GDALDataType datatype = band->GetRasterDataType();
        double data;
        for(int i=0;i<10;i++)
            band->RasterIO(GF_Read, i, i, 1, 1, &data, 1, 1, datatype, 0, 0);
        OGRCoordinateTransformation* ct = NULL;
        OGRSpatialReference mctRef;
        mctRef.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
        mctRef.importFromEPSG(3857);
        OGRSpatialReference wgsRef;
        wgsRef.SetAxisMappingStrategy(OAMS_TRADITIONAL_GIS_ORDER);
        wgsRef.importFromEPSG(4326);
        ct = OGRCreateCoordinateTransformation(&mctRef,&wgsRef);
        double x = 10, y=10;
        if (nullptr == ct)continue;
        ct->Transform(1, &x, &y);
        if (ct) 
            OGRCoordinateTransformation::DestroyCT(ct);


        GDALClose(dataset);
    }
    return 0;
}

int main(int argc, char** argv)
{
    if (argc != 2)
    {
        return -1;
    }
    const char * filename = argv[1];    
    const int numThreads = 10;
    GDALAllRegister();  
    for(int i=0; i < 100; i++)
    {

        std::thread threads[numThreads];

        for (int i = 0; i < numThreads; ++i)
        {
            threads[i] = std::thread(thread_test1, filename);
        }

        std::cout << "Hello from main thread!" << std::endl;
        
        for (int i = 0; i < numThreads; ++i)
        {
            threads[i].join();
        }

    }
    GDALDestroyDriverManager(); 
    return 0;
}

Versions and provenance

gdal3.7.0 from https://github.com/OSGeo/gdal/releases/tag/v3.7.0

Additional context

No response

@rouault
Copy link
Member

rouault commented Jan 8, 2025

@myBestLove I don't reproduce any issue with your test code and latest GDAL. Please try with GDAL master or 3.10 branch. I'd note your execution environment (CentOS 7 / gcc 4.8) is outdated. This might be a compiler issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants