Use C++ RAII for command line resource lifetime management#634
Merged
Conversation
There was a problem hiding this comment.
Pull request overview
This PR refactors the CLI/front-end resource management to use C++ RAII (e.g., std::unique_ptr, std::vector, and stream-based file I/O) instead of manual new[]/delete[] and alloc/free pairs, with the goal of eliminating leaks in error paths and improving maintainability.
Changes:
- Introduces RAII wrappers for
astcenc_contextandastcenc_image, and propagates these types through CLI image load/store and top-level processing. - Replaces raw compressed payload buffers (
uint8_t*+ length) withstd::vector<uint8_t>inastc_compressed_image. - Reworks several image container I/O paths (KTX/DDS and
.astc) to usestd::ifstream/std::ofstreamand standard containers.
Reviewed changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| Source/astcenccli_toplevel.cpp | Switches CLI pipeline to RAII-managed codec contexts and images; updates diagnostic and processing call sites. |
| Source/astcenccli_platform_dependents.cpp | Replaces manual new[]/delete[] for thread descriptors with std::vector. |
| Source/astcenccli_internal.h | Introduces RAII pointer aliases/deleters and updates API signatures to return smart pointers/vectors. |
| Source/astcenccli_image.cpp | Implements astcenc_image RAII allocation and deletion; converts float extraction to std::vector<float>. |
| Source/astcenccli_image_load_store.cpp | Converts multiple loaders/storers to RAII (vectors, smart buffers) and stream I/O; updates compressed image handling. |
| Source/astcenccli_image_external.cpp | Updates external PNG loader to return RAII-managed images. |
Comments suppressed due to low confidence (1)
Source/astcenccli_image_load_store.cpp:2206
- In the 8-bit DDS writer, the per-slice base pointer offset uses
dim_zinstead ofdim_x, which miscomputes the start of each Z-slice and will corrupt output (and can go out-of-bounds) for 3D images.
for (unsigned int z = 1; z < dim_z; z++)
{
row_pointers8[z] = row_pointers8[0] + dim_y * z;
row_pointers8[z][0] = row_pointers8[0][0] + dim_y * dim_z * image_components * z;
}
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The current front-end wrapper uses manual resource lifetime management with new/delete, alloc/free, and open/close pairs. It has always leaked in error paths, which makes using ASAN for genuine leak detection harder, especially when combined with fuzz testing.
We should just use C++ RAII given that the front-end is not a performance critical part of the codec, and it makes the entire thing more maintainable.
Fixes #631