destructor

Why is there no RAII in .NET?

若如初见. 提交于 2020-01-09 06:05:47
问题 Being primarily a C++ developer the absence of RAII (Resource Acquisition Is Initialization) in Java and .NET has always bothered me. The fact that the onus of cleaning up is moved from the class writer to its consumer (by means of try finally or .NET's using construct) seems to be markedly inferior. I see why in Java there is no support for RAII since all objects are located on the heap and the garbage collector inherently doesn't support deterministic destruction, but in .NET with the

Lifetime of object is over before destructor is called?

余生颓废 提交于 2020-01-09 05:07:45
问题 I don't understand this: 3.8/1 "The lifetime of an object of type T ends when: — if T is a class type with a non-trivial destructor (12.4), the destructor call starts , or — the storage which the object occupies is reused or released." If the lifetime ends before the destructor starts, doesn't that mean accessing members in the destructor is undefined behavior? I saw this quote too: 12.7 "For an object with a non-trivial destructor, referring to any non-static member or base class of the

Unresolved external in .obj files concerning FreeType library class destructors T::~T

末鹿安然 提交于 2020-01-06 16:22:57
问题 After having solved in a more or less good manner this problem : How to update a Borland 6 C++ Project from including indy.bpi to indy60.bpi? ... I now meet another difficulty : I now have "unresolved external" destuctors in .obj files : I have allready seen this error before : it seems to be a question of virtual destuctors that should be implemented with nothing : T::~T() { } ; (or = null;) The problem is that the concerned destructors are in the FreeType Library. I therefore suppose it to

PHP how to trigger user error with trigger_error in an object destructor while the script shuts down?

佐手、 提交于 2020-01-06 11:26:11
问题 While implementing some class I've run into a little problem: If the script ends and destructors are called because the script has finished, I wanted to trigger an error occasionally. I thought the trigger_error() function would be of use. However, if error_reporting(-1) the triggered error is not send any longer to STDOUT or STDERR - while it is expected to do so (e.g. if not within the __destructor/termination phase of the script, trigger_error works as expected). If I echo some message out

How do I Write a Custom Deleter to Handle Forward Declared Types

删除回忆录丶 提交于 2020-01-06 08:22:05
问题 Some older versions of visual-studio required a complete definition of the type for default_deleter to be defined. (I know this was the case in Visual Studio 2012 but I'd give bonus points to anyone who could tell me which version remedied this.) To work around this I can write a custom_deleter function like so: template <typename T> void custom_deleter(T* param) { delete param; } And declare a smart pointer on a forward declared type like so: unique_ptr<Foo, function<void(Foo*)>> pFoo ,

Utility functions for class constructors, destructor, and operator overloading

谁都会走 提交于 2020-01-05 05:25:11
问题 A while ago, I found in a website some code examples of utility functions that are used when creating , destructing objects, or even when overloading some of their operators . More precisely, the following member functions are mainly used: init, copy, set, and destroy. The init member function is used to initialize all the private members. It's mostly called inside the constructors , e.g. the default or parameter constructor . The copy member function is used to do a deep copy of an object

Python threading.Thread, scopes and garbage collection

那年仲夏 提交于 2020-01-03 18:54:08
问题 Say I derive from threading.Thread: from threading import Thread class Worker(Thread): def start(self): self.running = True Thread.start(self) def terminate(self): self.running = False self.join() def run(self): import time while self.running: print "running" time.sleep(1) Any instance of this class with the thread being started must have it's thread actively terminated before it can get garbage collected (the thread holds a reference itself). So this is a problem, because it completely

When does std::unique_ptr<A> need a special deleter if A has a destructor?

限于喜欢 提交于 2020-01-03 09:04:15
问题 If the class A in unique_ptr<A> it's own destructor, is it necessary to declare a deleter to ensure that the unique pointer uses that destructor? The example I am thinking of is that A has a member mx of type user_matrix (a name I just made up) which needs to call a function free(...) to release its memory, one would define ~A(){ user_matrix::free(mx); /*etc*/} Since default_deleter<> will call delete , it is my understanding that that should use ~A() . However, the example with the opening

In what kind of situation, c++ destructor will not be called? [closed]

亡梦爱人 提交于 2020-01-03 05:37:10
问题 As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 7 years ago . In c++, we love to do something in the destructor. But in what kind of situation, destructor will not be called? Examples in the

Disposing of SQL Connection

ぃ、小莉子 提交于 2020-01-02 07:15:09
问题 I have a SQL class that connects to the DB and retreives a DataTable. I am aware that the SqlConnection must be disposed when finished. I know this can be done using a using block, but is it also acceptable to put the Dispose() call inside the destructor of this class? Herre is my code: public class SQLEng { //Connection String Property //Must be set to establish a connection to the database public string ConnectionString{ get; set; } SqlConnection _Conn; //Overridden Constructor enforcing