Skip to content

Commit 41c0a2a

Browse files
committed
Fix a bug where SummaryKeyWordVector outlived its Summary
1 parent b92f60b commit 41c0a2a

2 files changed

Lines changed: 25 additions & 1 deletion

File tree

python/resdata/summary/rd_sum_keyword_vector.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ class SummaryKeyWordVector(BaseCClass):
1313

1414
def __init__(self, rd_sum, add_keywords=False):
1515
c_pointer = _rd_sum_keyword_vector._alloc(rd_sum, add_keywords)
16+
self._rd_sum = rd_sum
1617
super().__init__(c_pointer)
1718

1819
def __getitem__(self, index):
@@ -42,6 +43,8 @@ def __repr__(self):
4243
return self._create_repr("len=%d" % len(self))
4344

4445
def copy(self, rd_sum):
45-
return SummaryKeyWordVector.createPythonObject(
46+
cpy = SummaryKeyWordVector.createPythonObject(
4647
_rd_sum_keyword_vector._alloc_copy(self, rd_sum)
4748
)
49+
cpy._rd_sum = rd_sum
50+
return cpy

tests/rd_tests/test_sum.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import csv
22
import datetime
3+
import gc
34
import os
45
import os.path
56
import shutil
@@ -873,3 +874,23 @@ def test_that_end_time_is_sim_start_when_there_are_no_steps():
873874
assert summary.end_date == summary.start_date
874875
# Note that in this situation
875876
# sum.data_start will raise
877+
878+
879+
def test_that_summary_keyword_vector_lifetime_is_longer_than_summary():
880+
"""A regression test for a bug where SummaryKeyWordVector outlived its Summary"""
881+
882+
def _create():
883+
summary = createSummary(
884+
"CASE",
885+
[("FOPR", None, 0, "SM3")],
886+
sim_length_days=100,
887+
num_report_step=0,
888+
num_mini_step=0,
889+
sim_start=datetime.date(2010, 1, 1),
890+
func_table={},
891+
)
892+
return SummaryKeyWordVector(summary)
893+
894+
vector = _create()
895+
gc.collect()
896+
vector.add_keywords("FOPR")

0 commit comments

Comments
 (0)