Skip to content
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
2 changes: 1 addition & 1 deletion ext/sqlite3/aggregator.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ rb_sqlite3_aggregate_instance_destroy(sqlite3_context *ctx)
VALUE *inst_ptr = sqlite3_aggregate_context(ctx, 0);
VALUE inst;

if (!inst_ptr || (inst = *inst_ptr)) {
if (!inst_ptr || !(inst = *inst_ptr)) {
return;
}

Expand Down
21 changes: 21 additions & 0 deletions test/test_integration_aggregate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ def finalize
end
end

# Used by one test only, so a live count of 1 means just the template.
class ReleasableAggregator
def step(*args)
@sum ||= 0
args.each { |a| @sum += a.to_i }
end

def finalize
@sum
end
end

class AccumulateAggregator2
def step(a, b)
@sum ||= 1
Expand Down Expand Up @@ -396,6 +408,15 @@ def test_define_aggregator_does_not_use_moved_instances_after_gc_compaction
assert_equal 33, values[1]
end

def test_aggregate_instances_are_released_after_each_query
@db.define_aggregator("accumulate", ReleasableAggregator.new)

5.times { assert_equal 33, @db.get_first_value("select accumulate(c) from foo") }
GC.start(full_mark: true, immediate_sweep: true)

assert_equal 1, ObjectSpace.each_object(ReleasableAggregator).count
end

def test_step_on_statement_whose_database_was_closed_does_not_use_freed_aggregator
@db.define_aggregator("accumulate", AccumulateAggregator.new)
stmt = @db.prepare("select accumulate(c) from foo")
Expand Down
Loading