482 questions
1
vote
3
answers
123
views
Should reference counters always be used when creating a Tcl object?
There's a whole page on the Tcl wiki, but there are a lot of examples and it's hard for me to find them.
Here are two simple cases that I often use:
Tcl_Obj *innerObj = NULL;
innerObj = Tcl_NewListObj(...
0
votes
1
answer
103
views
Why does dropping a `&mut Rc<_>` do nothing? How to explicitly drop an `Rc<_>`?
I have a Scene struct holding resources for the program and Objects referencing resources of the scene and also being owned by the scene. So the resources are wrapped in Rc<>.
Rust playground
...
0
votes
0
answers
20
views
In OpenCL, do contexts keep subdevices alive?
In OpenCL (let's say v3.0), I know one can create contexts using sub-devices. But - what happens if you release all references to a sub-device while the context is not released (i.e. has positive ...
0
votes
1
answer
61
views
Why ctypes.c_long.from_address.value yields reference count of an object?
In this pycon conference the presenter says that ctypes.c_long.from_address(id(SOME_OBJECT)).value would yields the reference count of an object.
I tried to find this in the documentation but found ...
0
votes
1
answer
140
views
Is this atomic reference counting optimisation safe in C?
Is this code safe knowing that:
Header_mark_as_shared is always only called before an object crosses to other threads, no matter in which thread it resides in
The first call to Header_mark_as_shared ...
3
votes
2
answers
244
views
Is this a correct implementation of atomic reference counting in C?
Giving the following constraints, is the optimization in this code correct?
before an object is transfered to another thread, its reference count is incremented and is_shared is set to true.
...
0
votes
0
answers
44
views
Conceptual understanding: Specifying lifetimes for singular object owning references to its own data [duplicate]
I am still a bit newer to rust and have been getting some practice through Advent of code problems.
I am curious if anyone would be able to explain to my why the borrow checker is dislikes the ...
2
votes
1
answer
86
views
Why reference count of None object is fixed?
I was experimenting with refcount of objects, and I noticed the reference count of None object does not change when I bind identifiers to None. I observed this behavior in python version 3.13.
Python ...
1
vote
1
answer
121
views
What is the canonical way to read a value inside an Arc?
There is probably no way this is the first time someone has asked this question, but for the life of me I couldn't find this answer. So I want to ask this in the hope that someone will either harshly ...
2
votes
1
answer
102
views
Can DecRef be optimized with refcount.load(std::memory_order_relaxed) == 1?
IncRef:
refcount.fetch_add(1, std::memory_order_relaxed)
DecRef:
if (refcount.load(std::memory_order_relaxed) == 1 ||
refcount.fetch_sub(1, std::memory_order_release) == 1) {
std::...
0
votes
0
answers
52
views
Objective C ARC: _foo in constructor autoincrements or not?
a bit related to In Objective-C MRR, if foo is a retain property, then self.foo = [[Foo alloc] init] will need to be released immediately?
suppose you have
@property (nonatomic, readwrite, retain) ...
0
votes
1
answer
94
views
Can the pages obtained by get_user_pages() be directly recycled or swapped out without using put_user_pages()?
get_user_pages() increments the page reference count. And that is why it can pin the page in memory.
So I wonder whether the pages obtained by get_user_pages() can be directly recycled or swapped out ...
0
votes
2
answers
126
views
How can I avoid Rc<String> extra pointer indirection?
I'm writing a bit of code that uses Rc<String>. I'm pretty sure that using this would create an extra pointer indirection, with one pointer to the Rc, which has a pointer to String, which is a ...
6
votes
0
answers
136
views
Under what circumstance can `sys.getrefcount()` return zero?
I was reading the Python 3.12 official documentation on the sys module, and the part about getrefcount() seems rather confusing to me (emphasis mine).
Return the reference count of the object. The ...
0
votes
0
answers
51
views
Direct access to the internal fields of a CPython object
To improve my understanding of Cython, I am trying to find a way to directly access the fields of the objects defined by CPython.
For example, I wrote the following code to access the ob_item field of ...
1
vote
2
answers
304
views
how to clone an Rc from a method that takes in &self
I have a struct Region which contains [Option<Rc<RefCell<Region>>>; 2] as its subregions and Option<Weak<RefCell<Region>>> as its container. I am writing a method ...
2
votes
1
answer
73
views
Pytest/Mock keeping around extra object references in case of caught exceptions
I am running into a strange issue using pytest and mock: I am trying to create a call to __del__ by deleting an object using del .... According to the documentation, del only reduces the reference ...
1
vote
1
answer
105
views
Is it possible to access a variable from the enclosing scope inside a callback function in Rust without reference counting or mutexes?
I am an absolute beginner with rust, so apologies if my question is not phrased in the correct terms, I am happy to provide more clarification if needed.
I am writing a program using sdl and rlua. ...
0
votes
2
answers
88
views
IronPython garbage collection - How does it provides compatibility with C-extensions?
In this part of the talk on GIL by Larry Hastings, there is an explanation about how ironclad provides C-extension compatibility with IronPython. This is the interesting part of the talk:
We ...
1
vote
1
answer
103
views
Sharing objects implementing trait in Rust
I have a trait for objects that can provide bytes from some index. These could be files, processes being ptraced, caches over other byte providers, etc.:
use std::result::Result;
use std::io::Error;
...
0
votes
0
answers
91
views
COM programming reference count, release and destroy
Let's say I have allocated a string using SysAllocString. A separate class which is using the string calls SysAddRefString. Before it releases the string with SysReleaseString, the original owner ...
0
votes
1
answer
69
views
Reference Counting Using RCObjects vs. static inline C++
I am currently reading More Effective C++ (1995) by Scott Meyers.
In Item 29 - Reference Counting, the author metioned that the primary benefits of using reference counting are
(1) "Simplify ...
2
votes
0
answers
140
views
Reference count after deleting a literal
I am new to python and while I was counting the references to the number "3" in the context of embedding Python in C++ I found out a behavior that I was not expecting at all. Can someone ...
0
votes
1
answer
126
views
How to count references in java correctly?
I have difficulties understanding how to correctly count a reference variables.
Example (adopted from some book for java learning):
class Car {
}
class Example {
public static void main(String[] ...
4
votes
0
answers
156
views
How do I prevent an arbitrary object's destructor from ever running in Python?
Context
I have a troublesome object from third-party code in a Python (3.10) program. The object is not written in pure Python; it's provided by an extension module.
Due to a bug in the third party ...
1
vote
0
answers
274
views
In Swift, what is the accurate way to check the reference count of an class object in memory?
English is not my native language, so please excuse any typing errors. I am using a translator for my question.
I learned from watching "WWDC16 - Understanding Swift Performance" that when ...
7
votes
1
answer
771
views
In Rust, how does Weak<T> know when the inner value has been dropped?
std::rc::Weak<T> has the following definition:
pub struct Weak<T: ?Sized> {
ptr: NonNull<RcBox<T>>,
}
In my understanding, when there's no more Rc<T> left, RcBox<...
1
vote
2
answers
498
views
Why do Python objects without circular references still get removed by garbage collection?
If a function creates a lot of objects in a loop that are not referenced elsewhere, they will be removed immediately due to Python's reference counting.
If I store the objects in a list this time, the ...
0
votes
2
answers
159
views
TArray Result not always initially () within for loop?
Result in Test is NOT always initially ()
I have found Do I need to setLength a dynamic array on initialization?, however I do not fully understand that answer
More importantly, what is the best ...
2
votes
1
answer
264
views
Why does getrefcount increase by 2 when put inside a function?
Consider the following code:
import sys
a = [1, 2, 3]
def foo(x):
print(sys.getrefcount(x))
foo(a) # prints out 4 -- but why?
When we invoke foo(a) and when print(sys.getrefcount(x)) executes, ...
5
votes
2
answers
205
views
Comparing 2 method of C++ ref-counting
To write a ref-counted class, I have seen 2 different approaches:
Approach 1:
struct RefCounting1 {
void ref_up() {
m_ref.fetch_add(1, std::memory_order_relaxed);
}
void release() {
if (...
0
votes
1
answer
48
views
Design pattern for constructing resource at first thread arrival and destructing on last thread departure
I'm looking for a concurrency design pattern in C++ which enables a single resource instance to be constructed by the first thread which needs it, persist as long as at least one thread is accessing ...
-2
votes
1
answer
481
views
Why does cloning an Rc pointer in Rust increase its strong_count?
I have this function:
use std::rc::Rc;
fn rc_counter() -> Rc<String> {
let p1 = Rc::new(String::from("Hello"));
println!("count {}", Rc::strong_count(&p1)); //...
0
votes
1
answer
287
views
When using a ThreadPool passing Arc with reference gives a "data escapes the function body" error, passing Arc of data itself works fine
I've written some code to break up independent chunks of a computation into pieces and then feed each of the pieces to a thread pool which then does the computation and passes the result back the main ...
0
votes
0
answers
419
views
Using weak references with structs with lifetime parameters
I want to implement the observer pattern using Weak smart pointers. Ideally this would look something like the following (playground):
use std::cell::RefCell;
use std::rc::{Rc, Weak};
struct ...
1
vote
1
answer
163
views
Rust vector of struct instances with (non-circular) references to each other
I'd like to create a vector of TestStruct. TestStruct has an optional reference to another TestStruct instance. No TestStruct will ever reference itself, nor will there be circular references with the ...
1
vote
1
answer
536
views
Which memory management techniques exist?
I know of these three techniques:
Manually
uses
System.Classes;
procedure DoSomething;
var
sl: TStringList;
begin
sl := TStringList.Create;
try
finally
sl.Free; // Invoking destructor
...
0
votes
1
answer
413
views
How to do COM dll reference counting the right way? Official Microsoft samples are inconsistent
Some implementations calls DllAddRef() and DllRelease() in the CClassFactory constructor, destructor, and LockeServer member function:
https://github.com/microsoft/workbooks/blob/master/Clients/...
0
votes
1
answer
76
views
Peekable content is None after refactor the code
I have this method:
fn parse_http_request_headers<'b>(sp: &'b mut std::str::Split<&str>) -> HashMap<String, String> {
let mut headers: HashMap<String, String>...
1
vote
1
answer
181
views
how to count all pointers and variables that using an dynamically allocated memory with malloc and new?
if we override malloc and new function we can track first created pointer . but how can i track and count other pointers and functions that using this allocated memory ? (q pointer in below code ) ...
0
votes
1
answer
123
views
Does dereferencing a GArray unref it?
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <glib.h>
void transform_pointer_contents(GArray* const arr) {
GArray* const tmp = g_array_new(true, ...
0
votes
1
answer
485
views
Thread-safety of reference count in std::shared_ptr
Looking at this implementation of std::shared_ptr https://thecandcppclub.com/deepeshmenon/chapter-10-shared-pointers-and-atomics-in-c-an-introduction/781/ :
Question 1 : I can see that we're using std:...
0
votes
1
answer
347
views
Details of PHP 7.4 Reference Counting of Zvals
I have some points of clarification based on reading the internal implementations for zvals described here Internal value representation in PHP 7 - Part 1 and Internal value representation in PHP 7 - ...
0
votes
1
answer
118
views
Can python generational garbage collector kick in before objet is released by reference counting
In a CPython multithreaded environment, consider the following code
class Container:
def __del__(self):
# Some code that fails when run in a different thread than the thread that ...
2
votes
2
answers
290
views
Woes with std::shared_ptr<T>.use_counter()
https://en.cppreference.com/w/cpp/memory/shared_ptr/use_count states:
In multithreaded environment, the value returned by use_count is approximate (typical implementations use a memory_order_relaxed ...
4
votes
2
answers
244
views
Why does creating a variable name for an exception raised in a Python function affect the reference count of an input variable to that function?
I've defined two simple Python functions that take a single argument, raise an exception, and handle the raised exception. One function uses a variable to refer to the exception before raising/...
2
votes
1
answer
2k
views
Rc with zero references
Am I able to create an Rc with zero (strong) references? How to do this?
I am doing references counting digraphs. An empty digraph would be represented as an Rc variable with zero references.
If it is ...
5
votes
2
answers
418
views
Are generators with context managers an anti-pattern?
I'm wondering about code like this:
def all_lines(filename):
with open(filename) as infile:
yield from infile
The point of a context manager is to have explicit control over the lifetime ...
0
votes
2
answers
548
views
Do we need to use root = None during in the function? (python BST, reference counting)
I am practicing deleting node in BST and came across the following code.
def deleteNode(root, key):
if root is None:
return root
if key < root.key:
root.left = deleteNode(...
2
votes
1
answer
120
views
Why are references to python values, that are function parameters, stored on the stack(frame) in CPython?
Python works with reference counting. That means, if there is no more reference to a value, then the memory of that value is recycled. Or in other words. As long as there is at least one remaining ...