由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 怎么检测c++ smart pointer的循环引用?
相关主题
c++里 weak_ptr用起来是不是耗时间?那位再提醒我一下,reference的好处是?
int F::*x = &F::x是什么意思?a simple C++ question
C++ class cross reference problemsmart pointer 一问
Design pattern reflecting context change[c++] reference 真得不能bound to a second object 么?
some C++ interview questionsc++指针的问题
Garbage collection 问题琢磨了一下c++ smart pointer,发现不能到处用
ajax 页面为啥收不到response 呢?请教个JAVA的小问题
C++ interdependence question[合集] C++ question -- how to save objects
相关话题的讨论汇总
话题: object话题: context话题: gc话题: objects话题: 循环
进入Programming版参与讨论
1 (共1页)
N******K
发帖数: 10202
1
把std的smart pointer 继承了, 然后加入一个 object graph ? 类似于写一个GC...

还是有现成的方法?
k**********g
发帖数: 989
2

..
Yes, sort of.
The essence of this problem is to answer the question: "when is it safe to
permanently delete something".
If you answer is: "it is safe to delete it if it is theoretically impossible
to cause a problem by deleting it", then your answer is essentially what is
known as "reachability". Something that is not reachable is like
Schrodinger's cat; nobody knows if it is dead or alive.
I can think of three approaches. The first two are practical; the third one
is too advanced to be implemented by everyday programmers.
* Context object
* Database
* Your own GC implementation
----
A context object is the equivalent of a "GC root" in terms of reachability.
Every object you allocate must be registered with this context object.
What does it mean to be a "context" object? Consider a user enters a command
into the software. The "context" refers to the scope / lifetime of this
command. During this command's execution, some temporary objects will be
created and destroyed. These creation and destruction must be performed by
or registered with the context object. All other objects must use weak
pointers when referencing each other. This avoids the circular reference
issue between the objects.
When the command finishes, the context object is destroyed, ensuring that
all temporary objects created with it are also destroyed.
----
If the context object approach does not apply (e.g. some objects may have
infinite lifetime), then the database approach can be used.
----
Finally, if neither approach satisfies your requirement, you might need to
implement your own GC scheme. Please feel free to share your experiences on
this BBS.

【在 N******K 的大作中提到】
: 把std的smart pointer 继承了, 然后加入一个 object graph ? 类似于写一个GC...
:
: 还是有现成的方法?

N******K
发帖数: 10202
3
Contextobject 内部要有什么信息? object的有向图?
比如
Class A 有个成员变量m_classB, 是指向class B的指针
那么 是否要写
m_classB=ContextObject.CreatObj(this, new ClassB)
this指向Class A
这样就可以建立图 this是父节点 m_classB是子节点
有什么参考设计么?
database这个方法不太了解 能否讲讲?

impossible
is
one

【在 k**********g 的大作中提到】
:
: ..
: Yes, sort of.
: The essence of this problem is to answer the question: "when is it safe to
: permanently delete something".
: If you answer is: "it is safe to delete it if it is theoretically impossible
: to cause a problem by deleting it", then your answer is essentially what is
: known as "reachability". Something that is not reachable is like
: Schrodinger's cat; nobody knows if it is dead or alive.
: I can think of three approaches. The first two are practical; the third one

N******K
发帖数: 10202
4
如果 这个 context object 仅仅是包含了 所有类实例的shared_ptr 还是没有解决问题
如果有循环链接,这些类实例不会被delete,除非context object被delete了
如果有循环链接形成孤岛 要根据连接图进行删除操作 这样可以做到实时删除无用垃
圾 也就是context object要包含类实例的连接图 这样的话,就相当于一个GC
方案1:
每当删除一个类实例 更新连接图 检查一下是否有孤岛 如果有 就删除
问题: 每次都查图 费时
有什么比较好的方案?

impossible
is
one

【在 k**********g 的大作中提到】
:
: ..
: Yes, sort of.
: The essence of this problem is to answer the question: "when is it safe to
: permanently delete something".
: If you answer is: "it is safe to delete it if it is theoretically impossible
: to cause a problem by deleting it", then your answer is essentially what is
: known as "reachability". Something that is not reachable is like
: Schrodinger's cat; nobody knows if it is dead or alive.
: I can think of three approaches. The first two are practical; the third one

x****u
发帖数: 44466
5
用CLR扩展

..

【在 N******K 的大作中提到】
: 把std的smart pointer 继承了, 然后加入一个 object graph ? 类似于写一个GC...
:
: 还是有现成的方法?

N******K
发帖数: 10202
6
你用过?

【在 x****u 的大作中提到】
: 用CLR扩展
:
: ..

m*******l
发帖数: 12782
7
ft, why don't you use weak_ptr

【在 N******K 的大作中提到】
: 你用过?
N******K
发帖数: 10202
8
类之间的从属关系如果复杂怎么办? weak shared 混在一起

【在 m*******l 的大作中提到】
: ft, why don't you use weak_ptr
m*******l
发帖数: 12782
9
design. If that happens, re-design

【在 N******K 的大作中提到】
: 类之间的从属关系如果复杂怎么办? weak shared 混在一起
N******K
发帖数: 10202
10
我题目是 检测循环引用 就算设计阶段 也要检测
你难道用人肉法?

【在 m*******l 的大作中提到】
: design. If that happens, re-design
相关主题
Garbage collection 问题那位再提醒我一下,reference的好处是?
ajax 页面为啥收不到response 呢?a simple C++ question
C++ interdependence questionsmart pointer 一问
进入Programming版参与讨论
m*******l
发帖数: 12782
11
是不是因为你们滥用了shared_ptr?
这个理论上可以用wrapper, 但是其实人肉足够,如果设计好了

【在 N******K 的大作中提到】
: 我题目是 检测循环引用 就算设计阶段 也要检测
: 你难道用人肉法?

N******K
发帖数: 10202
12
还在设计阶段

【在 m*******l 的大作中提到】
: 是不是因为你们滥用了shared_ptr?
: 这个理论上可以用wrapper, 但是其实人肉足够,如果设计好了

C***y
发帖数: 2546
13
实现一memory allocator,专门在debug版本下面抓泄漏

【在 N******K 的大作中提到】
: 还在设计阶段
N******K
发帖数: 10202
14
有参考设计么?

【在 C***y 的大作中提到】
: 实现一memory allocator,专门在debug版本下面抓泄漏
C***y
发帖数: 2546
15
直接写个malloc和free的wrapper再加个hashmap就行了

【在 N******K 的大作中提到】
: 有参考设计么?
N******K
发帖数: 10202
16
根据 context object的想法 我轮了一个
方法1 测试:
for循环 100000000 (8个零) 次
{
new 一个 object
注册 object 到 context object
delete 这个 object
//自动从 context object 删除object记录
}
方法2 测试:
for循环 100000000 (8个零) 次
{
new 一个 object

delete 这个 object
}
方法3 测试:
for循环 100000000 (8个零) 次
{
生成一个 stack object
//{}结束 自动删除
}
release 模式 测试
时间
方法 1 : 48 秒
方法 2 : 6 秒
方法 3 : 1 秒
还可以 主要用来做计算 性能还可以

impossible
is
one

【在 k**********g 的大作中提到】
:
: ..
: Yes, sort of.
: The essence of this problem is to answer the question: "when is it safe to
: permanently delete something".
: If you answer is: "it is safe to delete it if it is theoretically impossible
: to cause a problem by deleting it", then your answer is essentially what is
: known as "reachability". Something that is not reachable is like
: Schrodinger's cat; nobody knows if it is dead or alive.
: I can think of three approaches. The first two are practical; the third one

1 (共1页)
进入Programming版参与讨论
相关主题
[合集] C++ question -- how to save objectssome C++ interview questions
弱问一下Garbage collection 问题
C++ Q05: pointer to constant variableajax 页面为啥收不到response 呢?
return value of a python function...C++ interdependence question
c++里 weak_ptr用起来是不是耗时间?那位再提醒我一下,reference的好处是?
int F::*x = &F::x是什么意思?a simple C++ question
C++ class cross reference problemsmart pointer 一问
Design pattern reflecting context change[c++] reference 真得不能bound to a second object 么?
相关话题的讨论汇总
话题: object话题: context话题: gc话题: objects话题: 循环