refcounting

How to avoid memory leak with CTFontCreateWithGraphicsFont?

吃可爱长大的小学妹 提交于 2021-01-27 06:19:28
问题 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 having to double release cg_font or changing order of the releases? Thanks. #include <stdio.h> #include <stdlib.h> #include <ApplicationServices/ApplicationServices.h> int main(int argc, char **argv) { FILE *f = fopen("/Library/Fonts/Tahoma.ttf", "rb"

string := const : why different implementation for local and result?

跟風遠走 提交于 2019-12-30 17:26:07
问题 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. http://docwiki.embarcadero.com/RADStudio/XE3/en/Internal_Data_Formats#Long_String_Types It really does suppress it: the code below does not leak. type TDealRecord = record id_Type: Integer; Price: extended; Remark: String; end; const const_loop = 100000000; function

Are memory barriers necessary for atomic reference counting shared immutable data?

╄→гoц情女王★ 提交于 2019-12-22 03:15:50
问题 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 (atomic_dec(p->refcount) == 0) { free(p->pit); free(p->juicy_innards); free(p); } } Does atomic_dec need a memory barrier in it? If so, what kind of memory barrier? Additional notes: The application must run on PowerPC and x86, so any processor-specific information is

How does a weak_ptr know that the shared resources has expired?

流过昼夜 提交于 2019-12-18 19:28:34
问题 Considering the following code: #include <memory> #include <iostream> using namespace std; struct MySharedStruct { int i; }; void print_value_of_i(weak_ptr<MySharedStruct> weakPtr) { if (shared_ptr<MySharedStruct> sp = weakPtr.lock()) { cout << "Value of i = " << sp->i << endl; } else { cout << "Resource has expired"; } } int main() { shared_ptr<MySharedStruct> sharedPtr(new MySharedStruct() ); sharedPtr->i = 5; weak_ptr<MySharedStruct> weakPtr; weakPtr = sharedPtr; print_value_of_i(weakPtr);

Is there a way to get the current ref count of an object in Python?

江枫思渺然 提交于 2019-12-17 10:46:56
问题 Is there a way to get the current ref count of an object in Python? 回答1: According to the Python documentation, the sys module contains a function: import sys sys.getrefcount(object) #-- Returns the reference count of the object. Generally 1 higher than you might expect, because of object arg temp reference. 回答2: Using the gc module, the interface to the garbage collector guts, you can call gc.get_referrers(foo) to get a list of everything referring to foo . Hence, len(gc.get_referrers(foo))

Why there need memory order limit on reference counter?

夙愿已清 提交于 2019-12-14 03:55:35
问题 In the example of boost::atomic , the unref function: void intrusive_ptr_release(const X * x) { if (x->refcount_.fetch_sub(1, boost::memory_order_release) == 1) { boost::atomic_thread_fence(boost::memory_order_acquire); delete x; } } 1: the fetch_sub op is limited by memory_order_release , which prevents preceding operations to be reordered past the point. But what are the possible scenes that would have such phenomenon? 2: in addition of memory_order_release on the atomic op, why there is an

Does accessing a list change its ref count?

泄露秘密 提交于 2019-12-11 13:27:16
问题 The original problem i am dealing with is outlined here. I would like to ask an additional question (about Python reference counting) related to the original problem. Lets say that i have the following script: from bitarray import bitarray from array import array list1=[bitarray('00011'), bitarray('00010'), bitarray('11011')] list2=[array('i',[0,0,0,0]),array('i',[1,1,1,1]),array('i',[2,2,2,2])] def calculate(l1,l2): result1=l1[0]&l1[1]&l1[2] result2=l2[0][0]+l2[1][1]+l2[2][2] return result1,

Missing shared_ref

耗尽温柔 提交于 2019-12-06 08:15:35
问题 While working with std::shared_ptr a lot I kind of miss a shared_ref implementation. That is a specialization of shared_ptr , which guarantees, that it never wraps a nullptr (given right usage, of course). I kind of wonder why it is not in the C++11 standard. Are there any mayor problems when implementing it? On the top of my head I cannot think of any. EDIT: I would expect to have an interface similar to: template <typename T> class shared_ref { public: shared_ref( T&& ref ); T& get(); T*

Simplest way to count instances of an object

隐身守侯 提交于 2019-11-30 22:55:05
问题 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 elements or something similar). Ofc I could have an atomic<int> cntMyObject; that I -- in destructor, ++ increase in constructor, cpy constructor(I hope I covered everything :)). But that is hardcoding for every class. And it is not simple do

How does a weak_ptr know that the shared resources has expired?

痞子三分冷 提交于 2019-11-30 18:11:14
Considering the following code: #include <memory> #include <iostream> using namespace std; struct MySharedStruct { int i; }; void print_value_of_i(weak_ptr<MySharedStruct> weakPtr) { if (shared_ptr<MySharedStruct> sp = weakPtr.lock()) { cout << "Value of i = " << sp->i << endl; } else { cout << "Resource has expired"; } } int main() { shared_ptr<MySharedStruct> sharedPtr(new MySharedStruct() ); sharedPtr->i = 5; weak_ptr<MySharedStruct> weakPtr; weakPtr = sharedPtr; print_value_of_i(weakPtr); sharedPtr.reset(new MySharedStruct() ); // <<----- How does weak_ptr know it has expired after this