Skip to content

Conversation

@xinjin01
Copy link
Contributor

Fix memory leaks and cleanup logic in the following sample programs:

  • samples/core/binaries
  • samples/core/blur
  • samples/core/reduce
  • samples/core/saxpy
  • samples/core/multi-device
  • samples/extensions/khr/externalmemory

Also fix minor logic and error-handling inconsistencies such as:

  • Ensuring proper release of cl_event objects
  • Correct ordering of Vulkan cleanup calls
  • Improved version string checks and CLI option allocation

Fix memory leaks and cleanup logic in the following sample programs:

- samples/core/binaries
- samples/core/blur
- samples/core/reduce
- samples/core/saxpy
- samples/core/multi-device
- samples/extensions/khr/externalmemory

Also fix minor logic and error-handling inconsistencies such as:
- Ensuring proper release of cl_event objects
- Correct ordering of Vulkan cleanup calls
- Improved version string checks and CLI option allocation

Signed-off-by: Xin Jin <xin.jin@arm.com>
@CLAassistant
Copy link

CLAassistant commented Jul 21, 2025

CLA assistant check
All committers have signed the CLA.

@bashbaug
Copy link
Contributor

I'll do a more thorough review once the CLA is signed, but I can confirm that I do not see any OpenCL object leaks after these changes (verified with the OpenCL Intercept Layer and LeakChecking). Thanks!

@xinjin01
Copy link
Contributor Author

thanks @bashbaug , I just signed the CLA.

Copy link
Contributor

@bashbaug bashbaug left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM.

Fix a use-after-free warning reported by GCC:

error: pointer 'dev_version' may be used after 'free'
       else if (opencl_version_contains(dev_version, "2."))

Previously, dev_version was freed immediately after checking for
OpenCL 1.0/1.1, but later reused for determining whether to add -cl-std
compiler options. This triggered -Werror=use-after-free under GCC with
strict diagnostics.

This patch:
- Defers free(dev_version) to a new 'ver:' cleanup label to avoid
  premature free.
- Replaces raw malloc/free with MEM_CHECK for safety and consistency.
- Adds snprintf-based -cl-std option handling with bounds checking.
- Moves compiler_options logic earlier to avoid stale pointer usage.
- Removes old compiler_options block near clBuildProgram.

The refactoring ensures correctness, eliminates UB, and prepares for
cleaner version-based behavior in the future.

Signed-off-by: Xin Jin <xin.jin@arm.com>
To address review comment

Signed-off-by: Xin Jin <xin.jin@arm.com>
Copy link
Contributor

@bashbaug bashbaug left a comment

Choose a reason for hiding this comment

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

Thanks! LGTM, aside from one nitpick.

Calling free() on a NULL pointer is well-defined and has no effect.
This simplifies the code by removing an unnecessary conditional.

Signed-off-by: Xin Jin <xin.jin@arm.com>
@bashbaug
Copy link
Contributor

bashbaug commented Aug 8, 2025

I filed an issue for the macOS CI failures, which seem unrelated to your changes: #144.

I'm going to merge these changes as-is.

Thank you again for the fixes!

@bashbaug bashbaug merged commit 6f25f5d into KhronosGroup:main Aug 8, 2025
1202 of 1261 checks passed
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.

3 participants