Skip to content

Use C++ RAII for command line resource lifetime management#634

Merged
solidpixel merged 12 commits into
mainfrom
issue_631
Jun 3, 2026
Merged

Use C++ RAII for command line resource lifetime management#634
solidpixel merged 12 commits into
mainfrom
issue_631

Conversation

@solidpixel
Copy link
Copy Markdown
Contributor

@solidpixel solidpixel commented Jun 2, 2026

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

@solidpixel solidpixel changed the title Use C++ streams to give RAII for image load file handles Use C++ RAII for command line resource lifetime management Jun 2, 2026
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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_context and astcenc_image, and propagates these types through CLI image load/store and top-level processing.
  • Replaces raw compressed payload buffers (uint8_t* + length) with std::vector<uint8_t> in astc_compressed_image.
  • Reworks several image container I/O paths (KTX/DDS and .astc) to use std::ifstream/std::ofstream and 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_z instead of dim_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.

Comment thread Source/astcenccli_toplevel.cpp Outdated
Comment thread Source/astcenccli_toplevel.cpp
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 20 out of 21 changed files in this pull request and generated 2 comments.

Comment thread Source/astcenccli_image_load_store.cpp Outdated
Comment thread Source/astcenccli_image_external.cpp
@solidpixel solidpixel merged commit aa3a673 into main Jun 3, 2026
9 checks passed
@solidpixel solidpixel deleted the issue_631 branch June 3, 2026 06:45
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

Successfully merging this pull request may close these issues.

Use scoped memory/resource management

2 participants