delete-operator

C++ destructor memory leak

Deadly 提交于 2020-01-24 13:05:05
问题 Relatively simple question about handling destructors properly... First I've got a class that's something like this: class Foo { public: ReleaseObjects() { for (std::map<size_t, Object*>::iterator iter = objects.begin(); iter != objects.end(); iter++) { delete (*iter).second; } objects.clear(); } private: std::map<size_t,Object*> objects; } So the function simply deletes the objects, which were created using 'new'. Problem is an Object class: class Bar : public Object { public: Bar() { baz =

Why operator void*() conversion function added to the C++ stream classes?

此生再无相见时 提交于 2020-01-22 17:30:29
问题 There is a conversion function operator void*() const in C++ stream classes. so that all stream objects can be implicitly converted to void* . During the interaction with programmers on SO they suggest me to don't use void* unless you've a good reason to use it. void* is a technique of removing type safety & error checking. So, due to the existance of this conversion function following program is perfectly valid. This is a flaw in the C++ standard library. #include <iostream> int main() {

Destructor in virtual inheritance

谁都会走 提交于 2020-01-20 08:41:27
问题 class Base{}; class D1:virtual public Base{}; class D2:virtual public Base{}; class DD:public D1,public D2{}; int main(){ Base *pBase=new DD; delete pBase; } This leads to crash, but I modify as below: class Base{ public: virtual ~Base(){}; }; class D1:virtual public Base{ public: virtual ~D1(){} }; class D2:virtual public Base{ public: virtual ~D2(){} }; class DD:public D1,public D2{ }; Then, it passes, but the default destructor should be the virtual dummy function, isn't it? 回答1: From the

How does =delete on destructor prevents allocation?

点点圈 提交于 2020-01-19 05:10:51
问题 In this SO question is stated that this construct prevents stack allocation of instance. class FS_Only { ~FS_Only() = delete; // disallow stack allocation }; My question is, how does it prevents allocation? I understand, that is not possible to delete this instance, either explicitly or implicitly. But I think, that would lead to memory leak or run time error, respectively. Are compilers smart enough to sort this out and raise compiler error? Also why would one need this? 回答1: The destructor

How does =delete on destructor prevents allocation?

99封情书 提交于 2020-01-19 05:09:25
问题 In this SO question is stated that this construct prevents stack allocation of instance. class FS_Only { ~FS_Only() = delete; // disallow stack allocation }; My question is, how does it prevents allocation? I understand, that is not possible to delete this instance, either explicitly or implicitly. But I think, that would lead to memory leak or run time error, respectively. Are compilers smart enough to sort this out and raise compiler error? Also why would one need this? 回答1: The destructor

How does =delete on destructor prevents allocation?

南楼画角 提交于 2020-01-19 05:09:16
问题 In this SO question is stated that this construct prevents stack allocation of instance. class FS_Only { ~FS_Only() = delete; // disallow stack allocation }; My question is, how does it prevents allocation? I understand, that is not possible to delete this instance, either explicitly or implicitly. But I think, that would lead to memory leak or run time error, respectively. Are compilers smart enough to sort this out and raise compiler error? Also why would one need this? 回答1: The destructor

Where/how to delete an object within another object, outside the function that it was created in

泪湿孤枕 提交于 2020-01-06 12:27:08
问题 In order to solve this problem Bad memory management? Class member (boolean) value greater than 1, in recursion function, I ran the whole program under Valgrind and found a few memory leak problems that occurred before the step. There were 2 'definitely lost' problems identified in the function CMFLoader. (here MyDataset is a vector of Molecule objects, and each Molecule object contains an Elements object) In CMFLoader::loadFile(vector& MyDataset), I originally have MyDataset.push_back(

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 ,

How to determine if a buffer is freed or not

别来无恙 提交于 2020-01-05 07:46:12
问题 I use new to allocate a buffer, as follows: BYTE *p; p = new BYTE[20]; ... delete p; After p is deleted, if I do NOT assign NULL to p , is there a way to determine whether it has already been freed? 回答1: You cannot determine that, and that's one of the main reasons you usually rely on higher-level mechanisms to handle memory, such as smart pointers (shared_ptr, unique_ptr and so on), or in your case rather std::vector , to deal with raw memory in a way that guarantee no double delete ,

Delete C++ structure from STL list using iterator

巧了我就是萌 提交于 2020-01-04 07:18:41
问题 I have this test program. I don't know how to delete struct in the list using iterator. #include<iostream> #include<list> using namespace std; typedef struct Node { int * array; int id; }Node; void main() { list<Node> nlist; for(int i=0;i<3;i++) { Node * p = new Node;//how to delete is later? p->array = new int[5];//new array memset(p->array,0,5*sizeof(int)); p->id = i; nlist.push_back(*p);//push node into list } //delete each struct in list list<Node>::iterator lt = nlist.begin(); while( lt