标签: reference-counting

为什么不收集TInterfacedObject垃圾的后代?

我有一个基于TInterfacedObject的类.我将它添加到TTreeNode的Data属性.

TFacilityTreeItem=class(TInterfacedObject)
private
  m_guidItem:TGUID;
  m_SomeOtherNode:TTreeNode;
public
end;
Run Code Online (Sandbox Code Playgroud)

我创建了这个对象的许多实例,并假设因为它们被引用计数,我不需要释放它们.那会很方便.

但是,在检查时,我打开了ReportMemoryLeaksOnShutdown,发现它们毕竟没有被释放.

这些对象是在放置在主窗体上的框架中创建的.在主窗体的FormClose中,我清除树节点,以便释放每个对象.

发生了什么?

谢谢您的帮助!

delphi reference-counting tinterfacedobject

4
推荐指数
1
解决办法
2511
查看次数

Delphi:从什么时候接口引用不再在with-block结束时释放?

我最近偶然发现了一些由我编写的非常旧的代码引起的问题,这显然假设一个with语句中使用的接口引用会在with-block被保留后立即释放- 有点像一个隐式try-finally块(类似于C#的using-statement)如果我理解正确的话).

显然(在Delphi 2009中)这不是(不再是?)的情况.有谁知道这发生的时间?或者我的代码开始时是完全错误的?

为了澄清,这是一个简化的例子:

type
  IMyIntf = interface;
  TSomeObject = class(TInterfacedObject, IMyIntf)
  protected
    constructor Create; override; // creates some sort of context
    destructor Destroy; override; // cleans up the context created in Create
  public
    class function GetMyIntf: IMyIntf; //a factory method, calling the constructor
  end;

procedure TestIt;
begin
  DoSomething;
  with (TSomeObject.GetMyIntf) do
    begin
      DoStuff;
      DoMoreStuff;
    end; // <- expected: TSomeObject gets destroyed because its ref.count is decreased to 0
  DoSomethingElse;
end; …
Run Code Online (Sandbox Code Playgroud)

delphi interface reference-counting delphi-2009 with-statement

4
推荐指数
1
解决办法
486
查看次数

多个字符串变量真的可以引用相同的数据吗?

根据互联网上的信息,我发现两个跟随变量指向内存中的相同位置.

任何人都可以提出一个代码示例来证明实际上它是真的(例如,通过更改第一个变量中的一个字母并看到这个变化在第二个变量中可见)?

procedure TForm1.Button1Click(Sender: TObject);
var
  a, b: String;
begin
  a := 'Test';
  b := a;

  showmessage (a);
  showmessage (b);
end;
Run Code Online (Sandbox Code Playgroud)

delphi string reference-counting

4
推荐指数
1
解决办法
504
查看次数

Java多线程:线程对象的引用计数变为零时的行为

[在我开始之前,我尝试搜索相关问题,因为我没有找到,我在这里问一个问题]

我正在学习Java,下面的场景让我头疼:

class MyThread extends Thread {
    void run(){
        //a heavy function
    }
}
Run Code Online (Sandbox Code Playgroud)

现在在主线程中,我将此线程调用为:

public static void main(...){
    new MyThread().start();
    System.gc(); //just a request..
    //end of main thread too.
    //Reference to that MyThread object is now zero.
}
Run Code Online (Sandbox Code Playgroud)

我运行了那段代码.似乎线程还活着.所有线程退出时程序结束.

我的问题:

  • 当引用计数为零时,该线程是否符合GC的条件?如果真的符合条件,那么垃圾收集的行为是什么?线程会被终止吗?
  • 我知道这是一件坏事,但它是否已被定义为无法otherThread.join()进入main()

我对自己有一些解释(但我不知道我是对的 - 我在这里发帖子的原因):

  • 只要JVM 处于活动状态, JVM就会保留对该线程的引用.因此引用计数永远不会为零.
  • 执行函数具有隐式this引用,因此引用计数也不为零.

我在上述任何解释中都是正确的吗?或者还有其他任何解释吗?

感谢致敬 :)

java multithreading garbage-collection reference-counting

4
推荐指数
1
解决办法
565
查看次数

引用计数(不存储任何数据)

我需要在我的班级中有一个共享计数器(当计数器变为零时调用某个函数).我可以使用shared_ptr<char>带有删除器的那个,但是这种方法有分配不需要的char并且保持指针的开销.

基本上,我需要引用计数的一部分shared_ptr.我不知道如何利用shared_ptr并避免这种开销.

std共享计数器是否有可移植的C++ 11实现(即,使用标准的c ++ 11,只有标准的互斥锁等)?

PS.计数器不是整个班级独有的.我可能有我班级的对象a1,a2,a3共享同一个计数器.与b1,b2,b3共享不同的计数器.因此,当a1,a2,a3中的最后一个超出范围时(与a1,a2,a3相关)应该发生.当b1,b2,b3中的最后一个超出范围时,应该发生某些事情(与b1,b2,b3相关).

谢谢

c++ reference-counting shared-ptr c++11

4
推荐指数
1
解决办法
249
查看次数

引用计数在shared_ptr中不起作用

以下代码导致运行时错误.

每个都shared_ptr拥有相同的内存,但每个内存的数量仍然是一个.

因此,每个共享指针都是不同的,所以当它们超出范围时,它们会尝试释放块,这会导致损坏堆.我的问题是如何避免这种情况?

只想添加这样的声明

shared_ptr<int> x(p);
Run Code Online (Sandbox Code Playgroud)

不可谈判我必须申报.

#include <iostream>
#include <memory>
using namespace std;
int main ()
{
  int* p = new int (10);
  shared_ptr<int> a (p);
  shared_ptr<int> b (p);
  shared_ptr<int> c (p);
  shared_ptr<int> d (p);
  cout<<"Count : "<<a.use_count()<<endl;
  cout<<"Count : "<<b.use_count()<<endl;
  cout<<"Count : "<<c.use_count()<<endl;
  cout<<"Count : "<<d.use_count()<<endl;
  return 0;
}
Run Code Online (Sandbox Code Playgroud)

c++ reference-counting shared-ptr

4
推荐指数
1
解决办法
299
查看次数

当对原始类型变量执行"="时,CPython实际上做了什么?

例如:

a = some_process_that_generates_integer_result()
b = a
Run Code Online (Sandbox Code Playgroud)

有人告诉我,ba将指向同一块整数对象,因此b将修改该对象的引用计数.代码PyObject* ast2obj_expr(void* _o)Python-ast.c中执行:

static PyObject* ast2obj_object(void *o)
{
    if (!o)
        o = Py_None;
    Py_INCREF((PyObject*)o);
    return (PyObject*)o;
}

......

case Num_kind:
    result = PyType_GenericNew(Num_type, NULL, NULL);
    if (!result) goto failed;
    value = ast2obj_object(o->v.Num.n);
    if (!value) goto failed;
    if (PyObject_SetAttrString(result, "n", value) == -1)
            goto failed;
    Py_DECREF(value);
    break;
Run Code Online (Sandbox Code Playgroud)

但是,我认为修改引用计数而不更改所有权实际上是徒劳的.我期望的是每个包含原始值(浮点数,整数等)的变量总是有自己的值,而不是引用同一个对象.

在执行我的简单测试代码时,我发现上面的Num_kind分支中的断点永远不会到达:

def some_function(x, y):
    return (x+y)*?x-y)

a = some_function(666666,66666)
print a

b = a …
Run Code Online (Sandbox Code Playgroud)

python cpython reference-counting

4
推荐指数
1
解决办法
145
查看次数

共享指针不会增加use_count

我试图了解如何std::shared_ptr在C++中使用.但它很混乱,我不明白如何创建指向同一对象的多个共享指针.即使是文档和在线资料也不是很清晰.

以下是我编写的一小段代码,用于尝试和理解其std::shared_ptr行为:

#include <iostream>
#include <memory>
using namespace std;

class Node
{
public:
    int key;
    Node()
    {
        key = 0;
    }
    Node(int k)
    {
        key = k;
    }
};

int main()
{
    Node node = Node(10);

    shared_ptr<Node> ptr1((shared_ptr<Node>)&node);
    cout << "Use Count: " << ptr1.use_count() << endl;

    // shared_ptr<Node> ptr2=make_shared<Node>(node);//This doesn't increase use_count
    shared_ptr<Node> ptr2((shared_ptr<Node>)&node);
    cout << "Use Count: " << ptr2.use_count() << endl;

    if (ptr1 == ptr2)
        cout << "ptr1 & ptr2 point to the …
Run Code Online (Sandbox Code Playgroud)

c++ reference-counting shared-ptr make-shared c++11

4
推荐指数
1
解决办法
512
查看次数

Python CApi 引用计数详细信息

我在这里查看一些示例代码(https://docs.python.org/2.0/api/refcountDetails.html)并试图更好地理解两个示例之间的差异:第一个示例是:

PyObject *t;

t = PyTuple_New(3);
PyTuple_SetItem(t, 0, PyInt_FromLong(1L));
PyTuple_SetItem(t, 1, PyInt_FromLong(2L));
PyTuple_SetItem(t, 2, PyString_FromString("three"));
Run Code Online (Sandbox Code Playgroud)

作者解释说 PyTuple_SetItem()窃取了引用(因此无需 DECREF)。 好吧,我明白了。然后作者使用 PySequence_SetItem() 提供了类似的代码,它不会窃取引用,因此调用者必须 DECREF,示例代码如下所示:

PyObject *l, *x;

l = PyList_New(3);
x = PyInt_FromLong(1L);
PySequence_SetItem(l, 0, x); Py_DECREF(x);
x = PyInt_FromLong(2L);
PySequence_SetItem(l, 1, x); Py_DECREF(x);
x = PyString_FromString("three");
PySequence_SetItem(l, 2, x); Py_DECREF(x);
PyObject *l, *x;
Run Code Online (Sandbox Code Playgroud)

我的问题是,如果第二个示例与第一个示例类似,传递 PyTYPE_FromSOMETYPE 如下,会发生什么?

PyObject *l;

l = PyList_New(3);
PySequence_SetItem(l, 0, PyInt_FromLong(1L));
PySequence_SetItem(l, 1, PyInt_FromLong(2L));
PySequence_SetItem(l, 2, PyString_FromString("three"));
Run Code Online (Sandbox Code Playgroud)

最后一种情况是良性的,还是会导致内存泄漏(因为 PySequence_SetItem 不会取得 PyInt_FromLong …

python reference-counting python-c-api

4
推荐指数
1
解决办法
903
查看次数

Is there an operation for Rc or Arc which clones the underlying value and returns it to the caller?

I'm looking for something roughly like this take, but atomic:

impl<T: Clone> for Arc<T> {
    fn take(mut self) -> T {
        Arc::make_mut(&mut self);
        Arc::try_unwrap(self).unwrap()
    }
}
Run Code Online (Sandbox Code Playgroud)

In other words, I want Arc::make_mut which returns the value itself, rather than a mutable reference.

smart-pointers reference-counting rust

4
推荐指数
1
解决办法
306
查看次数