Skip to content

Commit

Permalink
VSIGetMemFileBuffer(): delay destroying VSIMemFile on bUnlinkAndSeize (
Browse files Browse the repository at this point in the history
  • Loading branch information
mmomtchev authored Aug 8, 2021
1 parent 244a4dc commit ac50212
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
17 changes: 17 additions & 0 deletions autotest/cpp/test_cpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3486,4 +3486,21 @@ namespace tut

CPLQuadTreeDestroy(hTree);
}
// Test bUnlinkAndSize on VSIGetMemFileBuffer
template<>
template<>
void object::test<50>()
{
VSILFILE *fp = VSIFOpenL("/vsimem/test_unlink_and_seize.tif", "wb");
VSIFWriteL("test", 5, 1, fp);
GByte *pRawData = VSIGetMemFileBuffer("/vsimem/test_unlink_and_seize.tif", nullptr, true);
ensure(EQUAL(reinterpret_cast<const char *>(pRawData), "test"));
ensure(VSIGetMemFileBuffer("/vsimem/test_unlink_and_seize.tif", nullptr, false) == nullptr);
ensure(VSIFOpenL("/vsimem/test_unlink_and_seize.tif", "r") == nullptr);
ensure(VSIFReadL(pRawData, 5, 1, fp) == 0);
ensure(VSIFWriteL(pRawData, 5, 1, fp) == 0);
ensure(VSIFSeekL(fp, 0, SEEK_END) == 0);
CPLFree(pRawData);
VSIFCloseL(fp);
}
} // namespace tut
7 changes: 5 additions & 2 deletions gdal/port/cpl_vsi_mem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,8 +1034,11 @@ GByte *VSIGetMemFileBuffer( const char *pszFilename,
CPLDebug("VSIMEM", "VSIGetMemFileBuffer() %s: ref_count=%d (before)",
poFile->osFilename.c_str(), poFile->nRefCount);
#endif
CPLAtomicDec(&(poFile->nRefCount));
delete poFile;
poFile->pabyData = nullptr;
poFile->nLength = 0;
poFile->nAllocLength = 0;
if (CPLAtomicDec(&(poFile->nRefCount)) == 0)
delete poFile;
}

return pabyData;
Expand Down

0 comments on commit ac50212

Please sign in to comment.