Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add a warning if objects are finalized without being destroyed #12

Merged
merged 1 commit into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/AMGX.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,12 +116,8 @@ end
inc_refcount!(x::AMGXObject) = x.ref_count[] += 1
dec_refcount!(x::AMGXObject) = x.ref_count[] -= 1

function with(f, object::AMGXObject)
try
f(object)
finally
close(object)
end
function warn_not_destroyed_on_finalize(x::AMGXObject)
!(x.handle == C_NULL) && @async @warn("AMGX: likely memory leak: a `$(typeof(x))` was finalized without having been `close`d")
end


Expand Down
7 changes: 6 additions & 1 deletion src/Config.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
Base.@kwdef mutable struct Config <: AMGXObject
handle::API.AMGX_config_handle = C_NULL
handle::API.AMGX_config_handle = API.AMGX_config_handle(C_NULL)
ref_count::Threads.Atomic{Int} = Threads.Atomic{Int}(0)
function Config(handle::API.AMGX_config_handle, ref_count::Threads.Atomic{Int})
config = new(handle, ref_count)
finalizer(warn_not_destroyed_on_finalize, config)
return config
end
end
get_api_destroy_call(::Type{Config}) = API.AMGX_config_destroy
dec_refcount_parents(config::Config) = nothing
Expand Down
8 changes: 7 additions & 1 deletion src/Matrix.jl
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
# TODO: Add support for uploading a CuSparseMatrixBSR

Base.@kwdef mutable struct AMGXMatrix <: AMGXObject
handle::API.AMGX_matrix_handle = C_NULL
handle::API.AMGX_matrix_handle = API.AMGX_matrix_handle(C_NULL)
mode::Union{Mode, Nothing} = nothing
resources::Union{Resources, Nothing} = nothing
function AMGXMatrix(handle::API.AMGX_matrix_handle, mode::Union{Mode, Nothing},
resources::Union{Resources, Nothing})
m = new(handle, mode, resources)
finalizer(warn_not_destroyed_on_finalize, m)
return m
end
end
get_api_destroy_call(::Type{AMGXMatrix}) = API.AMGX_matrix_destroy
function dec_refcount_parents(m::AMGXMatrix)
Expand Down
8 changes: 7 additions & 1 deletion src/Resources.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
Base.@kwdef mutable struct Resources <: AMGXObject
handle::API.AMGX_resources_handle = C_NULL
handle::API.AMGX_resources_handle = API.AMGX_resources_handle(C_NULL)
ref_count::Threads.Atomic{Int} = Threads.Atomic{Int}(0)
cfg::Union{Config, Nothing} = nothing
function Resources(handle::API.AMGX_resources_handle, ref_count::Threads.Atomic{Int},
cfg::Union{Config, Nothing})
resources = new(handle, ref_count, cfg)
finalizer(warn_not_destroyed_on_finalize, resources)
return resources
end
end
get_api_destroy_call(::Type{Resources}) = API.AMGX_resources_destroy
function dec_refcount_parents(resources::Resources)
Expand Down
8 changes: 7 additions & 1 deletion src/Solver.jl
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
Base.@kwdef mutable struct Solver <: AMGXObject
handle::API.AMGX_solver_handle = C_NULL
handle::API.AMGX_solver_handle = API.AMGX_solver_handle(C_NULL)
resources::Union{Resources, Nothing} = nothing
config::Union{Config, Nothing} = nothing
bound_matrix::Union{AMGXMatrix, Nothing} = nothing
function Solver(handle::API.AMGX_solver_handle, resources::Union{Resources, Nothing},
config::Union{Config, Nothing}, bound_matrix::Union{AMGXMatrix, Nothing})
solver = new(handle, resources, config, bound_matrix)
finalizer(warn_not_destroyed_on_finalize, solver)
return solver
end
end
function dec_refcount_parents(solver::Solver)
dec_refcount!(solver.resources)
Expand Down
8 changes: 7 additions & 1 deletion src/Vector.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
Base.@kwdef mutable struct AMGXVector <: AMGXObject
handle::API.AMGX_vector_handle = C_NULL
handle::API.AMGX_vector_handle = API.AMGX_vector_handle(C_NULL)
mode::Union{Mode, Nothing} = nothing
resources::Union{Resources, Nothing} = nothing
function AMGXVector(handle::API.AMGX_vector_handle, mode::Union{Mode, Nothing},
resources::Union{Resources, Nothing})
v = new(handle, mode, resources)
finalizer(warn_not_destroyed_on_finalize, v)
return v
end
end
get_api_destroy_call(::Type{AMGXVector}) = API.AMGX_vector_destroy
function dec_refcount_parents(v::AMGXVector)
Expand Down