Skip to content

Commit d941588

Browse files
guitargeekdpiparo
authored andcommitted
[PyROOT] Retain C++ memory ownership for TStyle
Similar to 18a7c1f, since also any instantiated TStyle is owned by ROOT. Closes #16918.
1 parent 874758b commit d941588

File tree

3 files changed

+43
-4
lines changed

3 files changed

+43
-4
lines changed

bindings/pyroot/pythonizations/CMakeLists.txt

+5-4
Original file line numberDiff line numberDiff line change
@@ -80,32 +80,33 @@ set(py_sources
8080
ROOT/_facade.py
8181
ROOT/__init__.py
8282
ROOT/_numbadeclare.py
83+
ROOT/_pythonization/__init__.py
8384
ROOT/_pythonization/_cppinstance.py
8485
ROOT/_pythonization/_drawables.py
8586
ROOT/_pythonization/_generic.py
86-
ROOT/_pythonization/__init__.py
8787
ROOT/_pythonization/_pyz_utils.py
88-
ROOT/_pythonization/_rvec.py
8988
ROOT/_pythonization/_runtime_error.py
89+
ROOT/_pythonization/_rvec.py
9090
ROOT/_pythonization/_stl_vector.py
9191
ROOT/_pythonization/_tarray.py
9292
ROOT/_pythonization/_tclass.py
9393
ROOT/_pythonization/_tclonesarray.py
9494
ROOT/_pythonization/_tcollection.py
9595
ROOT/_pythonization/_tcomplex.py
9696
ROOT/_pythonization/_tcontext.py
97-
ROOT/_pythonization/_tdirectoryfile.py
9897
ROOT/_pythonization/_tdirectory.py
98+
ROOT/_pythonization/_tdirectoryfile.py
99+
ROOT/_pythonization/_tf1.py
99100
ROOT/_pythonization/_tfile.py
100101
ROOT/_pythonization/_tformula.py
101-
ROOT/_pythonization/_tf1.py
102102
ROOT/_pythonization/_tgraph.py
103103
ROOT/_pythonization/_th1.py
104104
ROOT/_pythonization/_titer.py
105105
ROOT/_pythonization/_tobject.py
106106
ROOT/_pythonization/_tobjstring.py
107107
ROOT/_pythonization/_tseqcollection.py
108108
ROOT/_pythonization/_tstring.py
109+
ROOT/_pythonization/_tstyle.py
109110
ROOT/_pythonization/_ttree.py
110111
ROOT/_pythonization/_tvector3.py
111112
ROOT/_pythonization/_tvectort.py
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Author: Jonas Rembser CERN 11/2024
2+
3+
################################################################################
4+
# Copyright (C) 1995-2024, Rene Brun and Fons Rademakers. #
5+
# All rights reserved. #
6+
# #
7+
# For the licensing terms see $ROOTSYS/LICENSE. #
8+
# For the list of contributors see $ROOTSYS/README/CREDITS. #
9+
################################################################################
10+
11+
from . import pythonization
12+
13+
14+
def _TStyle_Constructor(self, *args, **kwargs):
15+
"""
16+
Forward the arguments to the C++ constructor and retain ownership. This
17+
helps avoiding double deletes due to ROOT automatic memory management.
18+
"""
19+
self._cpp_constructor(*args, **kwargs)
20+
import ROOT
21+
22+
ROOT.SetOwnership(self, False)
23+
24+
25+
@pythonization("TStyle")
26+
def pythonize_tstyle(klass):
27+
28+
klass._cpp_constructor = klass.__init__
29+
klass.__init__ = _TStyle_Constructor

bindings/pyroot/pythonizations/test/memory.py

+9
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,15 @@ class foo {
4343
delta = after - before
4444
self.assertLess(delta, 16)
4545

46+
def test_tstyle_memory_management(self):
47+
"""Regression test for https://github.com/root-project/root/issues/16918"""
48+
49+
h1 = ROOT.TH1F("h1", "", 100, 0, 10)
50+
51+
style = ROOT.TStyle("NewSTYLE", "")
52+
groot = ROOT.ROOT.GetROOT()
53+
groot.SetStyle(style.GetName())
54+
groot.ForceStyle()
4655

4756
if __name__ == '__main__':
4857
unittest.main()

0 commit comments

Comments
 (0)