gdal icon indicating copy to clipboard operation
gdal copied to clipboard

Add managed pointer types for memory buffers

Open dbaston opened this issue 1 year ago • 0 comments

Feature description

Many memory allocations for working buffers are performed in GDAL with a C-style malloc function. This ensures that failed memory allocations do not cause an exception to be thrown and avoids the unneeded cost of initializing the buffer. Additionally, macros allow printing information about the attempted allocation that may assist the user or a developer in finding a workaround. However, this approach adds complexity by requiring verbose casting whenever memory is allocated (e.g., static_cast<float*>(VSI_CALLOC_VERBOSE(nXSize, sizeof(float)))) and requiring the use of a corresponding CPLFree function to release the memory.

This could be improved by either writing new versions of the VSI_MALLOC macros that return a unique_ptr, or by creating a custom CPLBuffer type that handles allocation for and access to a buffer. The use of std::source_location from C++20 (or a backport) may remove the need to use macros.

Additional context

No response

dbaston avatar May 30 '24 18:05 dbaston