g***j 发帖数: 1275 | 1 当constructor里面的某个member在initialization的时候fail了,到底会有memory
leak么?其他的已经完成的member到底由谁来释放?是系统自己自动释放还是需要在
catch exception之后用户写代码来自己释放?
我曾经看过说“C++里面没有partially constructed object, 只有full constructed
object”,难道不是说没有memory leak么?
为什么在Effective C++里面有这样一句话“if an exception is thrown during
construction of an object, any parts of the object that have already been
fully constructed are automatically destroyed”,这是不是说,只要throw
exception就够了,不需要在catch之后在代码里面释放? 如果这样,是不是说哪怕
fail也没有memory leak了?
总之,这个地方很confuse,谁来解释一下? | x***y 发帖数: 633 | 2 I have not used C++ for a long time, but I think I remeber this.
First of all, you can definitely having a try catch for constructor or
destructor
2nd of all, all the objects in the stack will be unwinded
3nd of all, the object on the heap will not. | s***0 发帖数: 117 | 3 If you throw during construction, you will get a leak (on whatever pointer
you allocated memory to).
You can avoid this by using smart pointers. | d**********x 发帖数: 4083 | 4 in ctor, whatever, it is okay
in dtor, exception must be caught internally.
【在 x***y 的大作中提到】 : I have not used C++ for a long time, but I think I remeber this. : First of all, you can definitely having a try catch for constructor or : destructor : 2nd of all, all the objects in the stack will be unwinded : 3nd of all, the object on the heap will not.
| d**********x 发帖数: 4083 | 5 已经完成的member会回滚的,但是如果你是new出来的,你必须要手动delete才行。
另外c++的异常处理就是个渣,直接disable了得了。。
constructed
【在 g***j 的大作中提到】 : 当constructor里面的某个member在initialization的时候fail了,到底会有memory : leak么?其他的已经完成的member到底由谁来释放?是系统自己自动释放还是需要在 : catch exception之后用户写代码来自己释放? : 我曾经看过说“C++里面没有partially constructed object, 只有full constructed : object”,难道不是说没有memory leak么? : 为什么在Effective C++里面有这样一句话“if an exception is thrown during : construction of an object, any parts of the object that have already been : fully constructed are automatically destroyed”,这是不是说,只要throw : exception就够了,不需要在catch之后在代码里面释放? 如果这样,是不是说哪怕 : fail也没有memory leak了?
| g**w 发帖数: 969 | 6 如果class的成员都是纯正100% object,那么不用担心leak问题。没有指针,不要用
char* s = new char[100]; 不要用OS API, 而是包装过的类库。 | s***0 发帖数: 117 | 7 Do you care about your application's footprint and performance?
【在 g**w 的大作中提到】 : 如果class的成员都是纯正100% object,那么不用担心leak问题。没有指针,不要用 : char* s = new char[100]; 不要用OS API, 而是包装过的类库。
|
|