destructor

C# out of scope objects not getting collected

余生颓废 提交于 2021-01-28 11:45:47
问题 I'm trying to build my first unit tests and have a class that increments and decrements an instance counter in it's constructor and destructor respectively. I have a test to make sure this works but it fails, it seems that other instances of the class from my other tests aren't having their destructor called when they go out of scope. public class Customer { public Customer() { ++InstanceCount; } public Customer(int customerId) { CustomerId = customerId; ++InstanceCount; } ~Customer() { -

Insert in unordered map calls constructor

无人久伴 提交于 2021-01-28 02:52:45
问题 In order to avoid duplication of elements, I'm building a class that holds elements and provide an acces to them. My elements ( DynLibrary ) are movable but not copyable class DynLibrary { public: DynLibrary() : _handle(nullptr) {} DynLibrary(const std::string& path) { DynLibrary::open(path); } DynLibrary(const DynLibrary&) = delete; DynLibrary(DynLibrary&&) = default; ~DynLibrary() { DynLibrary::close(); } ... } Those object are allocated in an unordered_map which key is the path that

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

爱⌒轻易说出口 提交于 2021-01-20 17:18:28
问题 I have searched and I'm unable to come up with any good reason to use python's __enter__ / __exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, and the with statement is great. But the counterpart to that is that any code in those blocks is only executed in that context. By using these instead of __init__ / __del__ I appear to be creating an implicit contract with callers that they

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

走远了吗. 提交于 2021-01-20 17:18:07
问题 I have searched and I'm unable to come up with any good reason to use python's __enter__ / __exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, and the with statement is great. But the counterpart to that is that any code in those blocks is only executed in that context. By using these instead of __init__ / __del__ I appear to be creating an implicit contract with callers that they

Python __enter__ / __exit__ vs __init__ (or __new__) / __del__

时间秒杀一切 提交于 2021-01-20 17:18:07
问题 I have searched and I'm unable to come up with any good reason to use python's __enter__ / __exit__ rather than __init__ (or __new__ ?) / __del__ . I understand that __enter__ / __exit__ are intended for use with the with statement as context managers, and the with statement is great. But the counterpart to that is that any code in those blocks is only executed in that context. By using these instead of __init__ / __del__ I appear to be creating an implicit contract with callers that they

When passing a class by-value, does the caller or callee call the destructor?

橙三吉。 提交于 2020-12-01 09:36:43
问题 Suppose that I have the following (trimmed down) code: class P { P(); P(const P&); ~P(); } void foo(P x) { ... } void bar() { P p{}; foo(p); // compiler uses P::(const P&) to construct the value for x ... // compiler calls P::~P() on p } The compiler must create a copy of p in order to call foo , so the caller invokes the copy constructor before the call. My question is, who is in charge of destroying this created object? There seem to be two valid choices: The callee (i.e. foo ) calls the

When passing a class by-value, does the caller or callee call the destructor?

戏子无情 提交于 2020-12-01 09:36:00
问题 Suppose that I have the following (trimmed down) code: class P { P(); P(const P&); ~P(); } void foo(P x) { ... } void bar() { P p{}; foo(p); // compiler uses P::(const P&) to construct the value for x ... // compiler calls P::~P() on p } The compiler must create a copy of p in order to call foo , so the caller invokes the copy constructor before the call. My question is, who is in charge of destroying this created object? There seem to be two valid choices: The callee (i.e. foo ) calls the