destructor

How to delete a function argument early?

☆樱花仙子☆ 提交于 2019-12-11 04:07:37
问题 I'm writing a function which takes a huge argument, and runs for a long time. It needs the argument only halfway. Is there a way for the function to delete the value pointed to by the argument if there are no more references to it? I was able to get it deleted as soon as the function returns, like this: def f(m): print 'S1' m = None #__import__('gc').collect() # Uncommenting this doesn't help. print 'S2' class M(object): def __del__(self): print '__del__' f(M()) This prints: S1 S2 __del__ I

Modifications in object __destruct() not saved PHP [duplicate]

白昼怎懂夜的黑 提交于 2019-12-11 03:15:15
问题 This question already has an answer here : Closed 7 years ago . Possible Duplicate: Pass reference to $this in constructor PHP I'm working on a simple PHP framework as a learning project. I've got a request object with a method called _execute() . In that method I (among other things) create an object called pageController, call a method on it, and remove the object using the following code: $controller = new $this->_controllerName($this); call_user_func(array($controller, $this->_methodName)

Why isn't my .net destructor called in this very simple scenario?

拥有回忆 提交于 2019-12-11 02:33:42
问题 I've got the following code : public class A { ~A() { Console.WriteLine("destructor"); } } public static A Aref; static void Main(string[] args) { Aref = new A(); int gen = GC.GetGeneration(Aref); Aref = null; GC.Collect(gen, GCCollectionMode.Forced); Console.WriteLine("GC done"); } I thought my Finalizer method would be called upon my call to GC.Collect, which is not the case. Can anyone explain me why ? 回答1: Finalizers aren't called before GC.Collect() returns. The finalizers are run in a

C++ - Is destructor called when a vector holds objects?

眉间皱痕 提交于 2019-12-11 00:23:21
问题 If I dynamically allocate objects of a class inside a vector, is the destructor for each object called if I use clear()? 回答1: Yes, they are all cleaned up properly. From this link: All the elements of the vector are dropped: their destructors are called, and then they are removed from the vector container, leaving the container with a size of 0. The [sequence.reqmts] section of the upcoming standard also makes this clear: a.clear() destroys all elements in a , invalidates all references,

In python 2.x should I call object.__del__?

牧云@^-^@ 提交于 2019-12-10 21:29:31
问题 In Python 3.x all classes are subclasses of object . In 2.x you have to explicitly state class MyClass(object) . And, as I'm trying to write as much 3.x compatible code as possible, I'm subclassing object . In my program, I'm using the __del__ method, and I wanted to know if I should be calling object.__del__(self) , or if that's magically taken care of? Thanks, Wayne EDIT: It appears there is some confusion what I mean - in the documents it states: If a base class has a __del__() method, the

C++ unique_ptr versus friend class private destructor

谁都会走 提交于 2019-12-10 20:10:44
问题 I have this arrangement: class LexedFile { friend class Lex; //... private: ~LexedFile(); }; class Lex { //... private: std::map<std::string, std::unique_ptr<LexedFile> > Files; }; A Lex is the sole creator of LexedFile objects and retains ownership of all the LexedFile objects it creates in a map. Unfortunately, the compiler complains mightily about this due to visibility rules from the map variable to the LexedFile destructor. I can fix that problem by making ~LexedFile() public, but of

C++ destructor not being called, depending on the linking order

♀尐吖头ヾ 提交于 2019-12-10 19:58:07
问题 I ran across this issue in my application after checking it for memory leaks, and discovered that some of my classes are not being destroyed at all. The code below is split into 3 files, it is supposed to implement a pattern called pimpl. The expected scenario is to have both Cimpl constructor and destructor print their messages. However, that's not what I get with g++. In my application, only constructor got called. classes.h: #include <memory> class Cimpl; class Cpimpl { std::auto_ptr<Cimpl

Php Destruct Called Twice

空扰寡人 提交于 2019-12-10 19:31:38
问题 The code below illustrates the destruct() being called twice. I'd like to know why? class A { function hi(){ echo 'hi'; } function __destruct(){ echo 'destroy'; } } class B{ public $this_ = ''; function __construct(){ $this->this_ = new A; } function __call($method, $params) { return call_user_func_array(array($this->this_, $method), $params); } } $b = new B; $b->__destruct(); output: destroydestroy EDIT Both zneak and TomcatExodus is correct. If I simply: [..code..] $b = new B; $b->_

Matrix class operator overloading,destructor problem

心已入冬 提交于 2019-12-10 19:09:46
问题 I was trying to write a matrix class which would be able to find inverse,adjoint,etc. of a square matrix of any order. The constructor initializes an identity matrix of order n(passed to it). class Matrix { int** elements; int order; public: Matrix& operator=(const Matrix& second_inp) { if(this->order!=second_inp.order) cout<<"The matrix cannot be assigned!!!\n"<<this->order<<"\n"<<second_inp.order; else { for(int i=0;i<this->order;i++) for(int j=0;j<this->order;j++) this->elements[i][j] =

GNU GCC (g++): Why does it generate multiple dtors?

我的未来我决定 提交于 2019-12-10 17:43:52
问题 Developing environment: GNU GCC (g++) 4.1.2 While I'm trying to investigate how to increase 'code coverage - particularly function coverage' in unit testing, I've found that some of class dtor seems to be generated multiple times. Does some of you have any idea on why, please? I tried and observed what I mentioned the above by using the following code. In "test.h" class BaseClass { public: ~BaseClass(); void someMethod(); }; class DerivedClass : public BaseClass { public: virtual