destructor

Assignement operator changing value of the assigned object

亡梦爱人 提交于 2019-12-24 10:57:02
问题 I implemented a class to handle some external functions (e.g of another DLL). This functions gives me an integer I can use as a handle. Here is the important part of my code: MyClass { public: MyClass() { handle = getHandlefromExternalFunction(); } ~MyClass { if(handle>0) freeHandleFromExternalFunction(handle); } MyClass& operator=(MyClass& other) { freeHandleFromExternalFunction(handle); handle = other.handle other.handle = 0; //Is this a bad idea? } private: int handle; } In my main

Proper Object Disposal In C++/CLI

自古美人都是妖i 提交于 2019-12-24 10:00:18
问题 Consider the following class: public ref class Workspace { protected: Form^ WorkspaceUI; SplitContainer^ WorkspaceSplitter; AvalonEditTextEditor^ TextEditor; ScriptOffsetViewer^ OffsetViewer; SimpleTextViewer^ PreprocessedTextViewer; ListView^ MessageList; ListView^ FindList; ListView^ BookmarkList; ListView^ VariableIndexList; TextBox^ VariableIndexEditBox; Label^ SpoilerText; ToolStrip^ WorkspaceMainToolBar; ToolStripButton^ ToolBarNewScript; ToolStripButton^ ToolBarOpenScript;

Copy constructor calls destructor c++

安稳与你 提交于 2019-12-24 09:18:27
问题 I have a test class of my to make my own string functions. I have a problem with the copy destructor. I have 2 strings: s1 and s2. I call the function s3 = s1 + s2; It first calls the operator+ function and when it's finished it calls the destructor. Because of this the string object in the operator= function is empty. How can I fix this? Destructor: String::~String() { if (this->str) delete[] str; str = NULL; len = 0; } Copy Constructor: String::String(const String& string) { this->len =

Scope(failure) in C++11?

被刻印的时光 ゝ 提交于 2019-12-24 01:12:36
问题 I wrote a very simple solution however someone laughed and found a flaw as shown here http://ideone.com/IcWMEf #include <iostream> #include <ostream> #include <functional> #include <exception> using namespace std; // Wrong scope(failure) class FailBlockT { typedef function<void()> T; public: T t; FailBlockT(T t) { this->t=t; } ~FailBlockT() { if (std::uncaught_exception()) { t(); } } }; struct Test { ~Test() { try { FailBlockT f([]() { cout << "failure" << endl; }); // there is no any

With JQuery, is it possible to have a function run when a DOM element calls .remove()?

我怕爱的太早我们不能终老 提交于 2019-12-24 00:34:09
问题 As the question states, what I'm attempting to do is have a function that is called when a DOM element is removed from the DOM, much like a destructor. I looked into unload, but from my understanding that's only called when the browser navigates away from the page. Thanks in advance! 回答1: I don't know if it's that what you're looking for. Not really pretty code, just to show what I would like to use: // Just for this script's sake, you'll want to do it differently var body = $("body"); var

How can I tell lint to track a custodial pointer to a vector?

扶醉桌前 提交于 2019-12-24 00:18:47
问题 I have some code that loops and news up some pointers and stores them in a vector: std::vector<InputBox*> m_octets; ... InputBox* octet = new InputBox(rect, title, touch_num); m_octets.push_back(octet); In the class destructor I for_each over m_octets and invoke the destructor for each pointer. I think this is all good. It all compiles and the unit tests pass. The problem is Gimpel's PC-lint doesn't like it. It sees that `octet' is a custodial pointer that has not been freed (Warning 429). I

freeing shared resources in Android app

廉价感情. 提交于 2019-12-23 20:20:03
问题 I'm writing an Android app that has both an Activity and a Service component. Furthermore, I've got a class encapsulating a resource that I am sharing in several places across both the Activity and the Service. My question is how I can figure out when to free the resource. As I understand it, Java does not have a notion of a destructor, so I can't just create a destructor in the shared object which will be called when there are no more references to the object. Android has functions such as

How to manually destroy member variables?

▼魔方 西西 提交于 2019-12-23 15:29:17
问题 I have a basic question on destructors. Suppose I have the following class class A { public: int z; int* ptr; A(){z=5 ; ptr = new int[3]; } ; ~A() {delete[] ptr;}; } Now destructors are supposed to destroy an instantiation of an object. The destructor above does exactly that, in freeing the dynamically alloctaed memory allocated by new. But what about the variable z ? How should I manually destroy it / free the memory allocated by z ? Does it get destroyed automatically when the class goes

Double free of child object after using the copy constructor

久未见 提交于 2019-12-23 14:19:54
问题 I am having trouble figuring out why (it seems like) an object is being destructed twice. If i create a object of a class (B) which contains an object of another class (A) and i copy this object. the copied object is destructed twice. Altough it looks like this. I am unable to figure out this output. I have created the following (minimum?) example which seems to trigger my issue: #include <stdio.h> #include <stdint.h> template <class T> class A { public: A() { myCtr = ++ctr; printf("class A

Double free of child object after using the copy constructor

限于喜欢 提交于 2019-12-23 14:19:09
问题 I am having trouble figuring out why (it seems like) an object is being destructed twice. If i create a object of a class (B) which contains an object of another class (A) and i copy this object. the copied object is destructed twice. Altough it looks like this. I am unable to figure out this output. I have created the following (minimum?) example which seems to trigger my issue: #include <stdio.h> #include <stdint.h> template <class T> class A { public: A() { myCtr = ++ctr; printf("class A