B*******g 发帖数: 1593 | 1 把两个class的 ctor dtor 去掉这题就有点意思了
is |
|
z****e 发帖数: 2024 | 2 俺说的,就是在析构Foo的时候,也就是ref=0,同一个shared_ptr要以递归形式,两次进入自己的dtor。
这两次,都会发现ref=0,然后delete,故而crash???
这个题目的报错,好像和如下代码是一样的。
{
type* p=new type();
shared_ptr sp1(p);
shared_ptr sp2(p);
} |
|
d****p 发帖数: 685 | 3 你这个例子是典型的shared ptr误用.
both sp1 and sp2 have their internal shared count object and each such
shared count has incremented the ref count to 1
after the current scope goes out, sp2 gets destructed first and its shared
count object decrements the ref count to zero and the p will be deleted.
now sp1 goes out of scope and this repeated again so there is double delete.
次进入自己的dtor。 |
|
p***o 发帖数: 1252 | 4 这么说吧,你的code会crash是因为 多次 进入了 同一个 shared_ptr的dtor。
不用shared_ptr也能复现这个取决于map实现的错误,用了之后让错误更隐蔽。
> |
|
t****t 发帖数: 6806 | 5 你说的是实现上的考虑, 我也同意. 但是标准的规定有不同的考虑, 标准里根本不会出
现如vtbl或vptr这样的字眼. 所以标准里说的是, 在ctor/dtor里用的是"自己"的版本.
在构造invoker的时候, invoker没有"自己"的版本, 也没有基类的版本可以继承, 那
用谁的呢? 只好undefine了. |
|
g*********s 发帖数: 1782 | 6 ctor CDummy was called 7 times. so n = 7.
afterwards, dtor ~CDummy was called once. so n = 6.
u should read some texbooks first. it seems ur understanding on c++ is
very elementary.
try c++ prime or the c++ programming language, both latest version. |
|
g*********s 发帖数: 1782 | 7 it seems if you initialize the mutex by pthread_mutex_init(), u will have to
call pthread_mutex_destroy().
what if you just use the const initializer?
in ctor:
_lock (PTHREAD_MUTEX_INITIALIZER);
in dtor:
pthread_mutex_destroy(&lock) // is this necessary? |
|
g*********s 发帖数: 1782 | 8 1. the default ctor is declared as private. but how about its copy ctor,
assignment, and dtor?
2. is the method of get_instance() required to be static? |
|
p*****d 发帖数: 80 | 9 是和singleton的定义没关系,所以我说的是编程习惯的问题,要考虑到用户可能误用
,应该在compile time杜绝这种可能,但如果s_instance是pointer则dtor/release是
feature而非bug,这就是区别所在。 |
|
h****e 发帖数: 2125 | 10
i though u said ctor and dtor should be private, right? i just didn't
put'em there. |
|
g*********s 发帖数: 1782 | 11 1. the default ctor is declared as private. but how about its copy ctor,
assignment, and dtor?
2. is the method of get_instance() required to be static? |
|
p*****d 发帖数: 80 | 12 是和singleton的定义没关系,所以我说的是编程习惯的问题,要考虑到用户可能误用
,应该在compile time杜绝这种可能,但如果s_instance是pointer则dtor/release是
feature而非bug,这就是区别所在。 |
|
h****e 发帖数: 2125 | 13
i though u said ctor and dtor should be private, right? i just didn't
put'em there. |
|
t****t 发帖数: 6806 | 14 dtor is not called for partially executed ctor. |
|
g*********s 发帖数: 1782 | 15 is the following true?
copy ctor, assignment, and dtor are always created by the compiler if they
are not declared by the user.
default ctor, however, is created by compiler only when there's not any
user defined
ctor.
just feel the last one is weird. why compiler cannot create a default ctor
when a copy ctor defined? |
|
a****l 发帖数: 8211 | 16 I don't think it is necessarily true. The reason is simple: compiler
optimization.So, if a function is needed, it will appear in the final code;
if not, it will be removed by any self-respecting compiler.
Even a dtor might not be needed. E.g, the class only has static members... |
|
c****p 发帖数: 6474 | 17 delete会调用class的dtor,free不会。
如果class有动态成员,用free是会杯具的 |
|
r****t 发帖数: 10904 | 18 prevent stack: private dtor
prevent heap: private new, new array, placement new operators |
|
q**p 发帖数: 147 | 19 class A{
public:
A(int* _pi,int _size):pi(_pi),size(_size){}
int* pi;
int size;
};
int a[] = {1,2,3};
A* ap = new A(a,3);
delete ap;
ap的pi指向了a的第一个元素,请问A的default dtor 会删除 a这个数组么? |
|
t****t 发帖数: 6806 | 20 if i am correct, shared_ptr is the default behaviour of java except GC is
performed immediately. so it's natural that shared_ptr is used the most.
auto_ptr is problematic anyway. with c++1x's moving semantics, unique_ptr
will becomes useful. current gcc implementation is not as good as
shared_ptr though, for example if T is incomplete, you may experience
problem with instantiating dtor and move (which includes move ctor and move
assignment). |
|
t****t 发帖数: 6806 | 21 if i am correct, shared_ptr is the default behaviour of java except GC is
performed immediately. so it's natural that shared_ptr is used the most.
auto_ptr is problematic anyway. with c++1x's moving semantics, unique_ptr
will becomes useful. current gcc implementation is not as good as
shared_ptr though, for example if T is incomplete, you may experience
problem with instantiating dtor and move (which includes move ctor and move
assignment). |
|
b***i 发帖数: 3043 | 22 看Synopsis
template class shared_ptr {
public:
typedef T element_type;
shared_ptr(); // never throws
template explicit shared_ptr(Y * p);
~shared_ptr(); // never throws
Effects:
If *this is empty, or shares ownership with another shared_ptr instance (use
_count() > 1), there are no side effects.
Otherwise, if *this owns a pointer p and a deleter d, d(p) is called.
Otherwise, *this owns a pointer p, and delete p is called.
可见,你
boost::shared_ptr create(){... 阅读全帖 |
|
|
c*******y 发帖数: 1630 | 24 这个比较麻烦,写一个class。
ctor里面调用一次clock,或者time
dtor里面再调用一次,print diff出来。
每次在函数体的开头定义一个实例就可以了。 |
|
t****t 发帖数: 6806 | 25 if you new/delete in ctor/dtor, 99.99% case you need to either provide, or
disable: copy constructor, copy assignment operator; and/or: move
constructor, move assignment operator. otherwise your code is crap. |
|
G*****7 发帖数: 1759 | 26 let me try to expand his cryptic "and virtual" macro:
there something along the lines of "if you have a virtual member func, 99.9%
you need to make dtor virtual." |
|
t****t 发帖数: 6806 | 27 楼主没有提供完整的代码, 所以我就大胆猜一下. 初学者常犯的错误是, 在ctor里分配
了内存, 在dtor里释放了内存, 但是没有提供copy ctor. 所以过程是这样的:
v1被传入v2.copy(...), 然后在这个过程里default copy ctor被调用生成一个新的临
时对象v1'. v1'和v1 share分配的内存. 然后在v2.copy(...)里v1或v1'的内存被deep
copy, 所以v2没有问题. 然后离开v2.copy(...), 这时临时对象v1'被摧毁, v1'分配的
内存(即v1分配的)被释放, 这时v1指向一块被释放的内存. 下次print()的时候就打印
了垃圾出来.
简单吧? |
|
d**********x 发帖数: 4083 | 28 请问为什么要保证异常不流出dtor。
请问何谓ABI不兼容,如何解决。
请问Koeing Lookup是在搞毛,为什么它会给std lib的扩展带来困难
前两个不许google,最后一个允许你google,十分钟内答出来我就承认C++水一点也不
深。
, |
|
f******y 发帖数: 2971 | 29 what is abi?
请问为什么要保证异常不流出dtor。请问何谓ABI不兼容,如何解决。请问Koeing
Lookup是在搞毛,为什么它会给std lib的扩展带来困难前两个不许google,最后.....
... |
|
d**********x 发帖数: 4083 | 30 这并非完全不可能,比如说qt就有对reflection的模拟
只是需要付出较大的代价罢了。比如说每个class都要从QObject继承,method都要用
INVOKABALE声明。
我是java新手,如果理解错了请指教:reflection就是收集了所有的class的metadata
,在运行时可以方便地以string的形式调用或者查询各种method(包括ctor和dtor)。
如果我没理解错的话,这些事情完全可以在c++中在编译期用某些trick来解决,上面Qt
的处理方式就是一个例子。
C++真正没法解决的是在runtime创建新的class,那才是动态语言的范畴。 |
|
d**********x 发帖数: 4083 | 31 请问为什么要保证异常不流出dtor。
请问何谓ABI不兼容,如何解决。
请问Koeing Lookup是在搞毛,为什么它会给std lib的扩展带来困难
前两个不许google,最后一个允许你google,十分钟内答出来我就承认C++水一点也不
深。
, |
|
f******y 发帖数: 2971 | 32 what is abi?
请问为什么要保证异常不流出dtor。请问何谓ABI不兼容,如何解决。请问Koeing
Lookup是在搞毛,为什么它会给std lib的扩展带来困难前两个不许google,最后.....
... |
|
d**********x 发帖数: 4083 | 33 这并非完全不可能,比如说qt就有对reflection的模拟
只是需要付出较大的代价罢了。比如说每个class都要从QObject继承,method都要用
INVOKABALE声明。
我是java新手,如果理解错了请指教:reflection就是收集了所有的class的metadata
,在运行时可以方便地以string的形式调用或者查询各种method(包括ctor和dtor)。
如果我没理解错的话,这些事情完全可以在c++中在编译期用某些trick来解决,上面Qt
的处理方式就是一个例子。
C++真正没法解决的是在runtime创建新的class,那才是动态语言的范畴。 |
|
E*******1 发帖数: 3464 | 34 本来都闪了,可实在是看不惯你这颠倒黑白的。
我这不是学你的口气,以一个学物理的装逼来比拟一下你的口吻嘛,注意我回帖的上下
文好不?
我没说你说的那些不好,不过请注意你说这些话的语气是不是装逼,我贴东西都是有上
下文的,准确的说我把你这个装逼帖子全贴出来了,你说话还能再得瑟点吗?说的就像
是你发明的一样,哈哈。所以我当时说你装逼,语气的成分占了8成
"请问为什么要保证异常不流出dtor。
请问何谓ABI不兼容,如何解决。
请问Koeing Lookup是在搞毛,为什么它会给std lib的扩展带来困难
前两个不许google,最后一个允许你google,十分钟内答出来我就承认C++水一点也不
深。
"
最后,我装比的两个问题,你估计也答不上来
?" |
|
t****t 发帖数: 6806 | 35 to use dynamic_cast, you need at least one virtual members. if you don't
have virtual member, make dtor virtual is a good choice.
and you forgot the (). |
|
p*a 发帖数: 592 | 36 先call B的destructor,打印2,然后call m_a的,打印5,然后call m_x的,打印3,
最后call B的父亲A的dtor,打印1,因为m_i已经被--过了。 |
|
q****x 发帖数: 7404 | 37 The code is from stack overflow. why the dtor must be defined in .cpp,
instead of .h? Moving it to .h will lead to a compilation error.
// A.hpp
#include
class B;
class A {
std::unique_ptr myptr;
// B::~B() can't be seen from here
public:
~A();
};
// A.cpp
#include "B.hpp"
// B.hpp has to be included, otherwise it doesn't work.
A::~A() = default; // without this line, it won't compile
// however, any destructor definiton will do. |
|
p***o 发帖数: 1252 | 38 struct Pack {A a; B b; C c;};
std::map> pack_map_;
shared_ptr doesn't need copy ctor and assignment, it only
need to see the dtor at the time when you new Pack.
There is no need to use delete directly almost in all cases today.
If you use one, then there is a problem with your design.
tuple。 |
|
t****t 发帖数: 6806 | 39 这好象根本不是什么问题, 必须override的标=0, 不必override的不标
如果没有必须override的, 则把dtor标=0 |
|
p***o 发帖数: 1252 | 40 那个例子其实你把dtor的定义放到cpp里就可以编译过。
只是例子本身问题很多,没啥参考价值。
declaration |
|
p***o 发帖数: 1252 | 41 前面说了,做个B.cpp,里面include A.h, B.h,再把B的dtor放到里面就好。 |
|
p***o 发帖数: 1252 | 42 scope 3你clear了vector,里面的shared_ptr析构,两个count都减到0,
所以会调A的dtor。 |
|
y**b 发帖数: 10166 | 43 实际代码就是这样的:.cpp包含两个.h。
但是class B{}里面既有定义,也有一些实现,这个有关系吗。主要是B.h里面有多个
辅助类,还有一些继承类,有点大,不然可以快速改写一下进行测试。
还有,将B的dtor从B.h移到B.cpp里面,反而出现编译错误:
error: definition of implicitly-declared B::~B()
你原来的目的,不用smart pointer,也可以通过编译,只需要在B.h里面
class A;//必须对称,A.h里面要有class B;
class B{定义,不要实现};
.cpp include 两个.h,实现。定义和实现分开即可。 |
|
b***i 发帖数: 3043 | 44 行为一样。你把智能指针复制到了vector里面增加了A的计数。如果你把ptrList.push_
back(ptr2)注释掉,就发现ptr2.reset()也同样调用dtor. |
|
M**8 发帖数: 25 | 45 I believe the answer is (a)
(C) cannot be the answer. To make destuctor virtual will change the program
behavior in terms of func calls. It won't make differences in terms of
funcationalities because otherwise the logics prior to changes would be
wrong. In short, to make dtors virtual won't make the program worse. |
|
M**8 发帖数: 25 | 46 How about changing MyClass from
class MyClass
{
public:
int x;
};
to
class MyClass
{
public:
MyClass() {x = 10; }
int x;
};
Do we need to recompile? Or put this question in a different way, can you
give an example that making a dtor virtual will trigger application to
recompile? |
|
M**8 发帖数: 25 | 47 How about changing MyClass from
class MyClass
{
public:
int x;
};
to
class MyClass
{
public:
MyClass() {x = 10; }
int x;
};
Do we need to recompile? Or put this question in a different way, can you
give an example that making a dtor virtual will trigger application to
recompile? |
|
M**8 发帖数: 25 | 48 Anyway, can you give an example that making a dtor virtual will trigger
application to recompile?
Hard to argue what the differences are b/w "add" and "replace" here :) |
|