Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11
Commits
19208fe9
Commit
19208fe9
authored
Oct 13, 2015
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
install a cleanup handler for nontrivial lambda closures
parent
28f98aa2
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
41 additions
and
0 deletions
+41
-0
example/example5.cpp
+25
-0
example/example5.py
+3
-0
example/example5.ref
+6
-0
include/pybind/pybind.h
+7
-0
No files found.
example/example5.cpp
View file @
19208fe9
...
...
@@ -72,4 +72,29 @@ void init_ex5(py::module &m) {
m
.
def
(
"test_callback2"
,
&
test_callback2
);
m
.
def
(
"test_callback3"
,
&
test_callback3
);
m
.
def
(
"test_callback4"
,
&
test_callback4
);
/* Test cleanup of lambda closure */
struct
Payload
{
Payload
()
{
std
::
cout
<<
"Payload constructor"
<<
std
::
endl
;
}
~
Payload
()
{
std
::
cout
<<
"Payload destructor"
<<
std
::
endl
;
}
Payload
(
const
Payload
&
)
{
std
::
cout
<<
"Payload copy constructor"
<<
std
::
endl
;
}
Payload
(
Payload
&&
)
{
std
::
cout
<<
"Payload move constructor"
<<
std
::
endl
;
}
};
m
.
def
(
"test_cleanup"
,
[]()
->
std
::
function
<
void
(
void
)
>
{
Payload
p
;
return
[
p
]()
{
/* p should be cleaned up when the returned function is garbage collected */
};
});
}
example/example5.py
View file @
19208fe9
...
...
@@ -24,6 +24,7 @@ from example import test_callback1
from
example
import
test_callback2
from
example
import
test_callback3
from
example
import
test_callback4
from
example
import
test_cleanup
def
func1
():
print
(
'Callback function 1 called!'
)
...
...
@@ -38,3 +39,5 @@ print(test_callback2(func2))
test_callback3
(
lambda
i
:
i
+
1
)
f
=
test_callback4
()
print
(
"func(43) =
%
i"
%
f
(
43
))
test_cleanup
()
example/example5.ref
View file @
19208fe9
...
...
@@ -12,3 +12,9 @@ Callback function 2 called : Hello, x, True, 5
5
func(43) = 44
func(43) = 44
Payload constructor
Payload copy constructor
Payload move constructor
Payload destructor
Payload destructor
Payload destructor
include/pybind/pybind.h
View file @
19208fe9
...
...
@@ -69,6 +69,7 @@ private:
void
*
data
=
nullptr
;
bool
is_constructor
=
false
,
is_method
=
false
;
short
keywords
=
0
;
void
(
*
free
)
(
void
*
ptr
)
=
nullptr
;
return_value_policy
policy
=
return_value_policy
::
automatic
;
std
::
string
signature
;
PyObject
*
class_
=
nullptr
;
...
...
@@ -242,6 +243,9 @@ private:
m_entry
=
new
function_entry
();
m_entry
->
data
=
new
capture
{
std
::
forward
<
Func
>
(
f
),
std
::
tuple
<
Extra
...
>
(
std
::
forward
<
Extra
>
(
extra
)...)
};
if
(
!
std
::
is_trivially_destructible
<
Func
>::
value
)
m_entry
->
free
=
[](
void
*
ptr
)
{
delete
(
capture
*
)
ptr
;
};
typedef
arg_value_caster
<
Arg
...
>
cast_in
;
typedef
return_value_caster
<
Return
>
cast_out
;
...
...
@@ -333,6 +337,9 @@ private:
static
void
destruct
(
function_entry
*
entry
)
{
while
(
entry
)
{
delete
entry
->
def
;
if
(
entry
->
free
)
entry
->
free
(
entry
->
data
);
else
operator
delete
(
entry
->
data
);
function_entry
*
next
=
entry
->
next
;
delete
entry
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment