destructor

Running method while destroying the object

寵の児 提交于 2019-12-05 12:20:13
A few days ago my friend told me about the situation, they had in their project. Someone decided, that it would be good to destroy the object of NotVerySafeClass in parallel thread (like asynchronously). It was implemented some time ago. Now they get crashes, because some method is called in main thread, while object is destroyed. Some workaround was created to handle the situation. Ofcourse, this is just an example of not very good solution, but still the question: Is there some way to prevent the situation internally in NotVerySafeClass (deny running the methods , if destructor was called

Should an abstract class' destructor be pure virtual?

廉价感情. 提交于 2019-12-05 12:18:34
问题 I think virtual alone is generally sufficient. Is there another reason to make it pure virtual than to force derived classes to implement their own destructor? I mean if you allocate something in your class' constructor you should impement your own destructor - if your class is derived or not. Doesn't count as answer as I already know: If you want your class abstract and it has no pure virtual functions - leave it to the destructor. Some more uses? 回答1: If you want your class abstract and it

Sample use of a C# Destructor

跟風遠走 提交于 2019-12-05 12:09:50
I'm reading about destructors in C# but I'm having trouble finding a decent use-case for it. Could someone provide an example of usage with an explanation? Much, much appreciated. Update The code sample in the book implements both a Desctuctor and a Dispose() method, see this code snippet from the book. class MyClass { bool disposed = false; // Disposal status public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } ~MyClass() { Dispose(false); } protected virtual void Dispose(bool disposing) { if (disposed == false) { if (disposing == true) { // Dispose the managed resources.

C++: Explicitly call destructor of template parameter's typedef

Deadly 提交于 2019-12-05 11:41:54
I have the following: template <typename X> struct A { typedef X _X; }; template <typename Y> struct B { // Y is struct A typename Y::_X x; void call_destructor () { x.~Y::_X(); // This doesn't work x.Y::~_X(); // This as well } }; which doesn't compile, saying that qualified type does not match destructor name Using the keyword typename before the call also does not work. However, the following does compile: template <typename Y> struct B { typename Y::_X x; typedef typename Y::_X __X; void call_destructor () { x.~__X(); // This works } }; Can someone explain to me why, and is there any way

Destructors and inheritance in C++?

梦想与她 提交于 2019-12-05 11:23:41
I use Borland C++ Builder. And i had o problem #include <Classes.hpp> class TMyObject : public TObject { __fastcall TMyObject(); __fastcall ~TMyObject();//I would like to inherite my destructor from TObject }; __fastcall TMyObject::TMyObject() : TObject()//it will inherited my constructor from TObject { } And for that new destructor that will inherite ~TObject ? __fastcall TMyObject::~TMyObject????????????? This can be solved at TObject 's level. Its destructor has to be virtual: #include <Classes.hpp> class TObject { __fastcall TObject(); virtual __fastcall ~TObject(); }; This way you can

VB6 Collection Remove Doesn't Fire Class_Terminate

北城以北 提交于 2019-12-05 10:51:05
I apologize in advance; this is a long question. I've tried to simplify as much as I can but it's still a bit more long-winded than I'd care to see. In some legacy code, we've got a VB6 collection. This collection adds objects via the .Add method and removes them via the .Remove method. However, via tracing I can see that sometimes when the .Remove is called it appears that the class terminate for the object isn't called. But it's not consistent; it happens only rarely and I can't isolate the circumstances under which it fails to fire the class terminate. Consider the following demonstration

how to wrtie destructor for class including a Union

房东的猫 提交于 2019-12-05 09:47:48
I have a class contain different type of variables like this. class Field { union DATATYPE { int intValue; double doubleValue; char* charValue; MyClass* MyClassValue; } Value; ~Field() { delete[] Value.charValue; delete Value.MyClassValue; } } This destructor gives error. Since some objects don't have charValue initialized, the attempting to delete it raised error. 来源: https://stackoverflow.com/questions/16025132/how-to-wrtie-destructor-for-class-including-a-union

What does empty destructor do?

非 Y 不嫁゛ 提交于 2019-12-05 09:29:22
I heard that empty destructor doesn't do anything and calling it doesn't remove the object. But in the code: #include <iostream> #include <set> class a { public: ~a() {} std::set <int> myset; }; int main() { a object; object.myset.insert(55); object.~a(); object.myset.insert(20); std::cout << object.myset.size(); } I get: " * glibc detected * /.app: double free or corruption (fasttop):" and then "ABORT". If it matters I have c++11 flag enabled. So what does empty constructor actually do? It does something and I read it doesn't. Your destructor may look empty, but it's actually destructing the

something like stackbased objects in c++ for javascript

自作多情 提交于 2019-12-05 09:15:01
Looking for a construct in javascript which works like the destructor in stackbased or local object in c++, e.g. #include <stdio.h> class M { public: int cnt; M() {cnt=0;} void inc() {cnt++;} ~M() {printf ("Count is %d\n", cnt);} }; ... {M m; ... m.inc (); ... m.inc (); } // here the destructor of m will printf "Count is 2"); so this means I am looking for a construct which does an action when its scope is ending (when it "goes out of scope"). It should be robust in the way that it does not need special action at end of scope, like that destructor in c++ does (used for wrapping mutex-alloc and

C++ abstract class destructor [closed]

怎甘沉沦 提交于 2019-12-05 09:02:06
Closed . This question needs details or clarity . It is not currently accepting answers. Want to improve this question? Add details and clarify the problem by editing this post . Closed 5 years ago . Is it good practice (and is it possible) to create an abstract class using only a pure virtual destructor in the parent class? Here's a sample class AbstractBase { public: AbstractBase () {} virtual ~AbstractBase () = 0; }; class Derived : public AbstractBase { public: Derived() {} virtual ~Derived() {} }; Otherwise how can I create an abstract class if the attributes and constructors of the