Skip to content

Commit

Permalink
Fixed error with dup and clone
Browse files Browse the repository at this point in the history
  • Loading branch information
ankane committed Jun 7, 2023
1 parent 4d3611a commit 660d6e3
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 12 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
## 0.3.0 (unreleased)

- Fixed error with `dup` and `clone`
- Dropped support for Ruby < 3

## 0.2.7 (2023-02-01)
Expand Down
6 changes: 3 additions & 3 deletions lib/lightgbm/booster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(params: nil, train_set: nil, model_file: nil, model_str: nil)
set_verbosity(params)
check_result FFI.LGBM_BoosterCreate(train_set.handle_pointer, params_str(params), @handle)
end
ObjectSpace.define_finalizer(self, self.class.finalize(handle_pointer))
ObjectSpace.define_finalizer(@handle, self.class.finalize(handle_pointer.to_i))

self.best_iteration = -1

Expand Down Expand Up @@ -179,9 +179,9 @@ def update
finished.read_int == 1
end

def self.finalize(pointer)
def self.finalize(addr)
# must use proc instead of stabby lambda
proc { FFI.LGBM_BoosterFree(pointer) }
proc { FFI.LGBM_BoosterFree(::FFI::Pointer.new(:pointer, addr)) }
end

private
Expand Down
12 changes: 3 additions & 9 deletions lib/lightgbm/dataset.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,6 @@ def feature_names=(feature_names)
def reference=(reference)
if reference != @reference
@reference = reference
free_handle
construct
end
end
Expand Down Expand Up @@ -110,9 +109,9 @@ def handle_pointer
@handle.read_pointer
end

def self.finalize(pointer)
def self.finalize(addr)
# must use proc instead of stabby lambda
proc { FFI.LGBM_DatasetFree(pointer) }
proc { FFI.LGBM_DatasetFree(::FFI::Pointer.new(:pointer, addr)) }
end

private
Expand Down Expand Up @@ -164,19 +163,14 @@ def construct

check_result FFI.LGBM_DatasetCreateFromMat(c_data, 1, nrow, ncol, 1, parameters, reference, @handle)
end
ObjectSpace.define_finalizer(self, self.class.finalize(handle_pointer)) unless used_indices
ObjectSpace.define_finalizer(@handle, self.class.finalize(handle_pointer.to_i)) unless used_indices

self.label = @label if @label
self.weight = @weight if @weight
self.group = @group if @group
self.feature_names = @feature_names if @feature_names
end

def free_handle
FFI.LGBM_DatasetFree(handle_pointer)
ObjectSpace.undefine_finalizer(self)
end

def dump_text(filename)
check_result FFI.LGBM_DatasetDumpText(handle_pointer, filename)
end
Expand Down
5 changes: 5 additions & 0 deletions test/booster_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@ def test_num_trees
assert_equal 100, booster.num_trees
end

def test_copy
booster.dup
booster.clone
end

private

def booster
Expand Down
5 changes: 5 additions & 0 deletions test/dataset_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,9 @@ def test_rover
label = data.delete("y")
LightGBM::Dataset.new(data, label: label)
end

def test_copy
regression_train.dup
regression_train.clone
end
end

0 comments on commit 660d6e3

Please sign in to comment.