From 2e243bc99b2a6940acde0fb55221217c0eba6f40 Mon Sep 17 00:00:00 2001 From: Tim Besard Date: Mon, 9 Sep 2024 17:56:02 +0200 Subject: [PATCH] Preserve buffers from GC collection when doing unsafe things. (#219) --- lib/device.jl | 4 ++-- lib/kernel.jl | 4 ++-- lib/platform.jl | 4 ++-- lib/program.jl | 4 ++-- test/behaviour.jl | 2 +- 5 files changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/device.jl b/lib/device.jl index 302b8d7b..243f2fdc 100644 --- a/lib/device.jl +++ b/lib/device.jl @@ -24,7 +24,7 @@ end clGetDeviceInfo(d, prop, 0, C_NULL, sz) chars = Vector{Cchar}(undef, sz[]) clGetDeviceInfo(d, prop, sz[], chars, C_NULL) - return unsafe_string(pointer(chars)) + return GC.@preserve chars unsafe_string(pointer(chars)) end if s === :profile return get_string(CL_DEVICE_PROFILE) @@ -123,7 +123,7 @@ end clGetDeviceInfo(d, CL_DEVICE_EXTENSIONS, 0, C_NULL, size) result = Vector{Cchar}(undef, size[]) clGetDeviceInfo(d, CL_DEVICE_EXTENSIONS, size[], result, C_NULL) - bs = unsafe_string(pointer(result)) + bs = GC.@preserve result unsafe_string(pointer(result)) return String[string(s) for s in split(bs)] end diff --git a/lib/kernel.jl b/lib/kernel.jl index f9d5bfbb..29656fef 100644 --- a/lib/kernel.jl +++ b/lib/kernel.jl @@ -337,7 +337,7 @@ function Base.getproperty(k::Kernel, s::Symbol) clGetKernelInfo(k, CL_KERNEL_FUNCTION_NAME, 0, C_NULL, size) result = Vector{Cchar}(undef, size[]) clGetKernelInfo(k, CL_KERNEL_FUNCTION_NAME, size[], result, C_NULL) - return unsafe_string(pointer(result)) + return GC.@preserve result unsafe_string(pointer(result)) elseif s == :num_args result = Ref{Cuint}() clGetKernelInfo(k, CL_KERNEL_NUM_ARGS, sizeof(Cuint), result, C_NULL) @@ -360,7 +360,7 @@ function Base.getproperty(k::Kernel, s::Symbol) if err == CL_SUCCESS && size[] > 1 result = Vector{Cchar}(undef, size[]) clGetKernelInfo(k, CL_KERNEL_ATTRIBUTES, size[], result, C_NULL) - return unsafe_string(pointer(result)) + return GC.@preserve result unsafe_string(pointer(result)) else return "" end diff --git a/lib/platform.jl b/lib/platform.jl index 7113dd68..61891e36 100644 --- a/lib/platform.jl +++ b/lib/platform.jl @@ -21,7 +21,7 @@ function Base.getproperty(p::Platform, s::Symbol) clGetPlatformInfo(p, string_properties[s], 0, C_NULL, size) result = Vector{Cchar}(undef, size[]) clGetPlatformInfo(p, string_properties[s], size[], result, C_NULL) - return unsafe_string(pointer(result)) + return GC.@preserve result unsafe_string(pointer(result)) end if s == :extensions @@ -29,7 +29,7 @@ function Base.getproperty(p::Platform, s::Symbol) clGetPlatformInfo(p, CL_PLATFORM_EXTENSIONS, 0, C_NULL, size) result = Vector{Cchar}(undef, size[]) clGetPlatformInfo(p, CL_PLATFORM_EXTENSIONS, size[], result, C_NULL) - return split(unsafe_string(pointer(result))) + return GC.@preserve result split(unsafe_string(pointer(result))) end return getfield(p, s) diff --git a/lib/program.jl b/lib/program.jl index 2b6c30a4..ec5b99ec 100644 --- a/lib/program.jl +++ b/lib/program.jl @@ -128,7 +128,7 @@ function Base.getproperty(p::Program, s::Symbol) src_len[] <= 1 && return nothing src = Vector{Cchar}(undef, src_len[]) clGetProgramInfo(p, CL_PROGRAM_SOURCE, src_len[], src, C_NULL) - return unsafe_string(pointer(src)) + return GC.@preserve src unsafe_string(pointer(src)) elseif s == :binary_sizes sizes = Vector{Csize_t}(undef, p.num_devices) clGetProgramInfo(p, CL_PROGRAM_BINARY_SIZES, sizeof(sizes), sizes, C_NULL) @@ -180,7 +180,7 @@ function Base.getproperty(p::Program, s::Symbol) clGetProgramBuildInfo(p, device, CL_PROGRAM_BUILD_LOG, 0, C_NULL, size) log = Vector{Cchar}(undef, size[]) clGetProgramBuildInfo(p, device, CL_PROGRAM_BUILD_LOG, size[], log, C_NULL) - log_dict[device] = unsafe_string(pointer(log)) + log_dict[device] = GC.@preserve log unsafe_string(pointer(log)) end return log_dict else diff --git a/test/behaviour.jl b/test/behaviour.jl index 9ec9e7d1..a76ae63c 100644 --- a/test/behaviour.jl +++ b/test/behaviour.jl @@ -21,7 +21,7 @@ cl.launch(kern, str_len, nothing, out_buf) h = cl.read(out_buf) - @test unsafe_string(pointer(h)) == hello_world_str + @test hello_world_str == GC.@preserve h unsafe_string(pointer(h)) end @testset "Low Level API Test" begin