destructor

very confused about what happens when we reinitialize the object in c++

这一生的挚爱 提交于 2020-01-17 15:12:10
问题 #include<iostream> #include<string.h> using namespace std; struct integer { int a; integer *next; integer *prev; }; integer *temp,*temp1,*temp2; class Myinteger { private: public: integer *head; int length; integer *tail; Myinteger() { cout<<"constructed started"<<"\n"; length=1; temp = new integer; head=temp; tail=temp; temp->prev=NULL; temp->next=NULL; temp->a=0; cout<<"constructed ended"<<"\n"; } Myinteger (string s) { cout<<"constructor started string "<<"\n"; int i=0,flag=0; temp=new

Finalizers and destructors, what's Wikipedia saying?

陌路散爱 提交于 2020-01-15 07:10:23
问题 As I understand there are two camps concerning this question - the first one thinks that finalizer is a destructor specific to C#. So they think that these two things are the same. The second camp thinks that there's a slight difference - written in Wikipedia - "the term “destructor” is typically used to mean a deterministically-invoked cleanup, whereas a “finalizer” runs when the garbage collector says to run it." But let me clarify something for myself. Deterministically-invoked cleanup? In

Why is this destructor being called immediately after creation?

痞子三分冷 提交于 2020-01-14 07:36:07
问题 I have the following class: class FixedByteStream { public: FixedByteStream() : size(0), address(NULL), existing(false) {} FixedByteStream(int length) : existing(false) { size = length; address = new char[length]; } FixedByteStream(int length, char* addr) : existing(true) { size = length; address = addr; } FixedByteStream(string* str, bool includeNull = false) : existing(true) { size = (*str).length(); address = const_cast<char*>((*str).c_str()); if (includeNull){ ++size; } } ~FixedByteStream

Why does the Finalize/Destructor example not work in .NET Core?

て烟熏妆下的殇ゞ 提交于 2020-01-12 06:55:08
问题 I'm trying to learn how finalization and destructor works in C#, I tried to run the code in the System.Object.Finalize example(code copy-pasted, no changes made), but the output is not the same as expected, it shows that the destructor is never called. The code is: using System; using System.Diagnostics; public class ExampleClass { Stopwatch sw; public ExampleClass() { sw = Stopwatch.StartNew(); Console.WriteLine("Instantiated object"); } public void ShowDuration() { Console.WriteLine("This

What's the best practice to prevent memory leak if an exception thrown in constructor?

旧街凉风 提交于 2020-01-12 04:41:33
问题 I know if an exception is thrown in constructor, destructor will not be called(simple class, no inheritance). So if an exception is thrown in constructor and there is a chance some heap memory is not cleaned up. So what's best practice here? let's assume I have to call some function in constructor and it may throw exception. Shall I always use shared pointer in this case? What's the alternatives? Thank you! 回答1: I would stick to the RAII idiom. If you avoid "naked" resources (like operator

What makes __destruct called twice in such a simple PHP code?

生来就可爱ヽ(ⅴ<●) 提交于 2020-01-11 10:24:05
问题 <?php class A { static private $_instance = null; static public function Init() { self::$_instance = new A(); } function __construct() { echo "__construct\n"; } function __destruct() { var_dump(debug_backtrace()); echo "__destruct\n"; } } $a = A::Init(); Normally, we should get following output: (Yes. I got this result in 2 different servers with PHP 5.2.10-2ubuntu6.10 and PHP 5.3.1) __construct array(1) { [0]=> array(5) { ["function"]=> string(10) "__destruct" ["class"]=> string(1) "A" [

Should I Treat Entity Framework as an Unmanaged Resource?

大憨熊 提交于 2020-01-11 08:29:05
问题 I am working with a class that uses a reference to EF in its constructor. I have implemented IDisposable , but I'm not sure if I need a destructor because I'm not certain I can classify EF as an unmanaged resource. If EF is a managed resource, then I don't need a destructor, so I think this is a proper example: public ExampleClass : IDisposable { public ExampleClass(string connectionStringName, ILogger log) { //... Db = new Entities(connectionStringName); } private bool _isDisposed; public

c# my destructor isn't being called?

旧街凉风 提交于 2020-01-11 06:17:28
问题 I have this simple code and trying to call the destructor but I can't call it :( I know that GarbageCollector runs when it's necessary, so I used GC.WaitForPendingFinalizers(); but it didn't work either. Here is my code: class Program { static void Main(string[] args) { Calculator calculator = new Calculator(); Console.WriteLine("{0} / {1} = {2}", 120, 15, calculator.Divide(120, 15) GC.Collect(); GC.WaitForPendingFinalizers(); Console.WriteLine("Program finishing"); } } class Calculator { //

When does the compiler provide definitions for the special members of a class?

拟墨画扇 提交于 2020-01-11 03:18:06
问题 I know that when I define an empty class and provide no declarations at all, the compiler will provide definitions for the default and copy constructor, destructor and copy assignment operator. What are the rules for that? When does the compiler not provide a, say, copy constructor? What about the move constructor and move assignment operator? (Example: The compiler will not provide definitions for any assignment operator if my class has a reference member like int& . When else will something

When I kill a pThread in C++, do destructors of objects on stacks get called?

寵の児 提交于 2020-01-10 18:44:48
问题 I'm writing a multi-threaded C++ program. I plan on killing threads. However, I am also using a ref-counted GC. I'm wondering if stack allocated objects get destructed when a thread gets killed. 回答1: The stack does not unwind when you 'kill' a thread. Killing threads is not a robust way to operate - resources they have open, such as files, remain open until the process closes. Furthermore, if they hold open any locks at the time you close them, the lock likely remains locked. Remember, you