Questions tagged [refcounting]
56 questions
133
votes
5 answers
Is there a way to get the current ref count of an object in Python?
Is there a way to get the current ref count of an object in Python?
AJ
20
votes
9 answers
Simplest way to count instances of an object
I would like to know the exact number of instances of certain objects allocated at certain point of execution. Mostly for hunting possible memory leaks(I mostly use RAII, almost no new, but still I could forget .clear() on vector before adding new…

NoSenseEtAl
- 28,205
- 28
- 128
- 277
16
votes
3 answers
Are memory barriers necessary for atomic reference counting shared immutable data?
I have some immutable data structures that I would like to manage using reference counts, sharing them across threads on an SMP system.
Here's what the release code looks like:
void avocado_release(struct avocado *p)
{
if…

Dietrich Epp
- 205,541
- 37
- 345
- 415
15
votes
2 answers
How does a weak_ptr know that the shared resources has expired?
Considering the following code:
#include
#include
using namespace std;
struct MySharedStruct
{
int i;
};
void print_value_of_i(weak_ptr weakPtr)
{
if (shared_ptr sp = weakPtr.lock())
{…

Samaursa
- 16,527
- 21
- 89
- 160
11
votes
1 answer
Python disable reference counting for some objects
This question is derived from here.
I have three large lists containing python objects (l1, l2 and l3). These lists are created when the program starts and they take total of 16GB of RAM. The program will be used on linux exclusively.
I do not need…

FableBlaze
- 1,785
- 3
- 16
- 21
9
votes
2 answers
string := const : why different implementation for local and result?
In Delphi function result is frequently implemented as var-parameter (not out-parameter despite QC ticket).
String constants are basically variables with negative refcounter, which should suppress automatic memory [de]allocation.…

Arioch 'The
- 15,799
- 35
- 62
8
votes
1 answer
Making a reference-counted object in D using RefCounted!(T)
How do you use std.typecons.RefCounted!(T) to make a reference-counted object in D?
I've tried to figure out what std.array.Array does internally by looking at the source, but while I can read the source, I just can't figure what a "payload" is or…

user541686
- 205,094
- 128
- 528
- 886
8
votes
1 answer
How do reference-counting smart pointer's avoid or handle reference-counter overflows?
In a naive reference-counting smart pointer implementation, the reference-counter could overflow. How is this overflow avoided or handled in C++ standard library implementations?

sergej
- 17,147
- 6
- 52
- 89
8
votes
5 answers
Why the refcount is 2 not 1?
$var = 1;
debug_zval_dump($var);
Output:
long(1) refcount(2)
$var = 1;
$var_dup = &$var;
debug_zval_dump($var);exit;
Output :
long(1) refcount(1)
UPDATE
Very disapointed at the answer...

ajx
- 117
- 1
- 4
7
votes
7 answers
C++: Multithreading and refcounted object
I'm currently trying to pass a mono threaded program to multithread. This software do heavy usage of "refCounted" objects, which lead to some issues in multithread. I'm looking for some design pattern or something that might solve my problem.
The…

Steve Gury
- 15,158
- 6
- 38
- 42
7
votes
3 answers
Smart Pointers and Ref Counting in Java
I'm trying to write DagNode class in Java whereby two nodes are logically equal iff they are equal as references.
The idea in C++ —(I'm from C++)— would be to use smart pointers and reference counting:
When a node is created, I'll look up in some…

Joseph Victor
- 819
- 6
- 16
6
votes
3 answers
Why is my Python C Extension leaking memory?
The function below takes a python file handle, reads in packed binary data from the file, creates a Python dictionary and returns it. If I loop it endlessly, it'll continually consume RAM. What's wrong with my RefCounting?
static PyObject*…

Mark
- 106,305
- 20
- 172
- 230
6
votes
3 answers
why does a call to locals() add a reference?
I don't understand the below behavior.
How does locals() result in a new reference?
Why doesn't gc.collect remove it? I didn't assign the result of locals() anywhere.
x
import gc
from sys import getrefcount
def trivial(x): return x
def…

bukzor
- 37,539
- 11
- 77
- 111
5
votes
2 answers
Another question on thread safe ref counting
There are tons of questions on how to implement thread safe reference counters.
And a common highly voted answer is: "use atomic increment/decrements".
Ok, this is a good way to read and write refCounter whitout other thread changing it in between.…

Anton Petrov
- 782
- 1
- 8
- 19
5
votes
1 answer
How to avoid memory leak with CTFontCreateWithGraphicsFont?
I've reduced a leak issue to this easy to compile code which shows after CTFontCreateWithGraphicsFont use and release ct_font, an extra ref to cg_font will be left. Is this an internal Apple ref count issue or am I missing something around like…

Ebrahim Byagowi
- 10,338
- 4
- 70
- 81