mirror of
https://github.com/pybind/pybind11.git
synced 2026-05-12 01:10:34 +00:00
Improve constructor/destructor tracking
This commit rewrites the examples that look for constructor/destructor calls to do so via static variable tracking rather than output parsing. The added ConstructorStats class provides methods to keep track of constructors and destructors, number of default/copy/move constructors, and number of copy/move assignments. It also provides a mechanism for storing values (e.g. for value construction), and then allows all of this to be checked at the end of a test by getting the statistics for a C++ (or python mapping) class. By not relying on the precise pattern of constructions/destructions, but rather simply ensuring that every construction is matched with a destruction on the same object, we ensure that everything that gets created also gets destroyed as expected. This replaces all of the various "std::cout << whatever" code in constructors/destructors with `print_created(this)`/`print_destroyed(this)`/etc. functions which provide similar output, but now has a unified format across the different examples, including a new ### prefix that makes mixed example output and lifecycle events easier to distinguish. With this change, relaxed mode is no longer needed, which enables testing for proper destruction under MSVC, and under any other compiler that generates code calling extra constructors, or optimizes away any constructors. GCC/clang are used as the baseline for move constructors; the tests are adapted to allow more move constructors to be evoked (but other types are constructors much have matching counts). This commit also disables output buffering of tests, as the buffering sometimes results in C++ output ending up in the middle of python output (or vice versa), depending on the OS/python version.
This commit is contained in:
@@ -9,51 +9,55 @@
|
||||
*/
|
||||
|
||||
#include "example.h"
|
||||
#include "constructor-stats.h"
|
||||
#include <pybind11/operators.h>
|
||||
#include <pybind11/stl.h>
|
||||
|
||||
class Sequence {
|
||||
public:
|
||||
Sequence(size_t size) : m_size(size) {
|
||||
std::cout << "Value constructor: Creating a sequence with " << m_size << " entries" << std::endl;
|
||||
print_created(this, "of size", m_size);
|
||||
m_data = new float[size];
|
||||
memset(m_data, 0, sizeof(float) * size);
|
||||
}
|
||||
|
||||
Sequence(const std::vector<float> &value) : m_size(value.size()) {
|
||||
std::cout << "Value constructor: Creating a sequence with " << m_size << " entries" << std::endl;
|
||||
print_created(this, "of size", m_size, "from std::vector");
|
||||
m_data = new float[m_size];
|
||||
memcpy(m_data, &value[0], sizeof(float) * m_size);
|
||||
}
|
||||
|
||||
Sequence(const Sequence &s) : m_size(s.m_size) {
|
||||
std::cout << "Copy constructor: Creating a sequence with " << m_size << " entries" << std::endl;
|
||||
print_copy_created(this);
|
||||
m_data = new float[m_size];
|
||||
memcpy(m_data, s.m_data, sizeof(float)*m_size);
|
||||
}
|
||||
|
||||
Sequence(Sequence &&s) : m_size(s.m_size), m_data(s.m_data) {
|
||||
std::cout << "Move constructor: Creating a sequence with " << m_size << " entries" << std::endl;
|
||||
print_move_created(this);
|
||||
s.m_size = 0;
|
||||
s.m_data = nullptr;
|
||||
}
|
||||
|
||||
~Sequence() {
|
||||
std::cout << "Freeing a sequence with " << m_size << " entries" << std::endl;
|
||||
print_destroyed(this);
|
||||
delete[] m_data;
|
||||
}
|
||||
|
||||
Sequence &operator=(const Sequence &s) {
|
||||
std::cout << "Assignment operator: Creating a sequence with " << s.m_size << " entries" << std::endl;
|
||||
delete[] m_data;
|
||||
m_size = s.m_size;
|
||||
m_data = new float[m_size];
|
||||
memcpy(m_data, s.m_data, sizeof(float)*m_size);
|
||||
if (&s != this) {
|
||||
delete[] m_data;
|
||||
m_size = s.m_size;
|
||||
m_data = new float[m_size];
|
||||
memcpy(m_data, s.m_data, sizeof(float)*m_size);
|
||||
}
|
||||
|
||||
print_copy_assigned(this);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
Sequence &operator=(Sequence &&s) {
|
||||
std::cout << "Move assignment operator: Creating a sequence with " << s.m_size << " entries" << std::endl;
|
||||
if (&s != this) {
|
||||
delete[] m_data;
|
||||
m_size = s.m_size;
|
||||
@@ -61,6 +65,9 @@ public:
|
||||
s.m_size = 0;
|
||||
s.m_data = nullptr;
|
||||
}
|
||||
|
||||
print_move_assigned(this);
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user