.NET分析器可以显示对托管对象的引用计数。他们如何计算它们?
.net garbage-collection memory-management reference-counting
我最近有以下内存错误,这很容易在这里找到,但在更复杂的代码中可能更难检测:
class Foo : public IFoo {
const Bar& bar_;
public:
Foo(const Bar& bar) : bar_(bar) {
}
void test() {
// access bar_ here
}
};
int baz() {
IFoo* foo = NULL;
if(whatever) {
Bar bar;
foo = new Foo(bar);
}
else {
// create other foo's
}
foo->test(); // segmentation fault
}
Run Code Online (Sandbox Code Playgroud)
错误是Bar立即超出范围,被销毁然后使用foo->test().一种解决方案是Bar使用在堆上创建Bar* bar = new Bar().但是,我不喜欢这样做因为我必须将Bar* bar指针保持在顶层,所以我可以delete在最后访问它,即使Bar是特定于特定代码块的东西if(whatever){}.
另一个解决方案是 …
我正在编写一个程序,出于性能原因使用共享内存(套接字和管道作为替代品已被评估,并且它们对我的任务来说不够快,一般来说任何涉及副本的IPC方法都太慢).在共享内存区域中,我正在编写许多固定大小的结构.有一个程序负责将结构写入共享内存,以及许多从中读取的客户端.但是,每个结构中都有一个客户端需要写入的成员(引用计数,它们将以原子方式更新).所有其他成员都应该只读给客户.
由于客户端需要更改该成员,因此无法将共享内存区域映射为只读.但他们也不应该修改其他成员,因为这些程序是用C++编写的,所以内存损坏是可能的.理想情况下,一个客户端应该尽可能地使另一个客户端崩溃.我只担心有问题的客户,而不是恶意客户,所以允许不完美的解决方案.
我可以尝试通过声明他们用作const的标头中的成员来阻止客户端覆盖,但这不会阻止内存损坏(缓冲区溢出,错误的强制转换等)被覆盖.我可以插入金丝雀,但是我必须经常支付检查它们的费用.
我可以在一个单独的映射只写页面中存储指向实际数据的指针,而不是直接存储引用计数成员,同时将结构保留在只读映射页面中.这将起作用,如果我尝试写入指向的数据,操作系统将强制我的应用程序崩溃,但是在尝试编写无锁算法时间接存储可能是不合需要的,因为需要遵循另一级别的间接可以改变是否可以原子地完成.
有没有办法标记较小的内存区域,以便写入它们会导致您的应用程序爆炸?有些平台有硬件观察点,也许我可以激活其中一个内联汇编,但我只能在32位x86上一次只限4个,每个只能覆盖部分结构,因为它们有限到4个字节.这也使我的程序调试痛苦;)
编辑:我发现这张相当令人眼花缭乱的纸张,但不幸的是它需要使用ECC内存和修改过的Linux内核.
c++ reference-counting shared-memory virtual-memory memory-corruption
我真的很困惑.
// initial class
type
TTestClass =
class( TInterfacedObject)
end;
{...}
// test procedure
procedure testMF();
var c1, c2 : TTestClass;
begin
c1 := TTestClass.Create(); // create, addref
c2 := c1; // addref
c1 := nil; // refcount - 1
MessageBox( 0, pchar( inttostr( c2.refcount)), '', 0); // just to see the value
end;
Run Code Online (Sandbox Code Playgroud)
它应该显示1,但它显示0.无论我们将执行多少分配,值都不会改变!为什么不?
我正在查看其他人的代码,但它似乎是RELEASE对象视频,但随后继续使用它.
现在从我对面向对象编程语言的理解,一旦它被发布,它应该从内存中释放出来......
我看不出它有什么参考......但我认为这是没关系的原因.看起来像是一件奇怪的事情,(当你没有完成它时释放它,为什么不使用autorelease例如).
self.video = [[VideoFrameExtractor alloc] initWithVideo:[Utilities bundlePath:@"sophie.mov"]];
[video release];
// set output image size
video.outputWidth = 426;
video.outputHeight = 320;
Run Code Online (Sandbox Code Playgroud) STLPort是否通过引用计数机制实现了字符串?
我使用Delphi 2007,它几乎完全打动了界面引用计数.这个小代码块显示了问题:
program intf;
{$APPTYPE CONSOLE}
uses
Classes;
type
IMyIntf = interface(IInterface)
['{3DE76B13-1F8D-4BCE-914E-7E3B7FB0FA5A}']
function GetSelf: TObject;
end;
TMyObj = class(TInterfacedObject, IMyIntf)
private
FI: Integer;
public
constructor Create(i: Integer);
function GetSelf: TObject;
property I: Integer read FI;
end;
var
i, j: Integer;
il: TInterfaceList;
ii: IInterface;
MyObj: TMyObj;
IMyObj: IMyIntf;
constructor TMyObj.Create(i: Integer);
begin
inherited Create;
FI := i;
end;
function TMyObj.GetSelf: TObject;
begin
Result := Self;
end;
begin
// create list of interfaced objects and populate it
il := TInterfaceList.Create; …Run Code Online (Sandbox Code Playgroud) 如何在 JavaScript 中实现引用计数?目的是在创建第一个实例或销毁最后一个实例时触发构造函数/析构函数。
我需要实现三种类:抽象类、单例类和普通类(继承或不继承)。
我尝试实现包含所有实例数组的 ClassDispatcher 类,但删除它们并不会删除引用...
Module = function(parentModule)
{
this.destroyInstance = function()
{
Module.prototype.referenceCounter--;
}
this.initInstance = function()
{
Module.prototype.referenceCounter++;
}
this.initInstance();
}
Module.prototype.referenceCounter = 0;
Run Code Online (Sandbox Code Playgroud) javascript constructor destructor reference-counting javascript-objects
我有一个关于结构的问题
WWDC2016,session推荐使用struct(值类型)
但如果结构体还有 3 个内联变量字,则结构体必须管理引用计数以将大值存储到堆
那么我的问题是
当结构体有 3 个另一个结构体并且每个结构体有 2 或 3 个另一个结构体或值类型时
我想知道在这种情况下是否使用引用计数它是如何工作的
下面是结构体的例子
struct ViewModel {
var titleModel: TitleModel
var contentModel: ContentModel
var layoutModel: LayoutModel
}
struct TitleModel {
var text: String
var width: Float
var height: Float
}
struct ContentModel {
var content: String
var width: Float
var height: Float
}
struct LayoutModel {
var constant: Float
var multiply: Float
}
Run Code Online (Sandbox Code Playgroud) 我有一个对其委托有弱引用的类。在后台操作中,我需要设置委托,对类执行操作,然后释放委托。
下面的代码在调试模式下工作,但在发布模式下失败,因为在发布模式下委托会立即被释放。
protocol DocumentDelegate:class { ... }
class MyDocument {
weak var delegate:DocumentDelegate?
func save() {
assert(self.delegate =! nil)
}
}
// Later:
// (#1) Using "var" does not work:
var delegate:DocumentDelegate? = InterimDelegate()
let document = MyDocument()
document.delegate = delegate
// Compiled in Release mode, at this time the delegate is already nil!
document.save()
delegate = nil
// (#2) Using "let" does work:
let delegate:DocumentDelegate = InterimDelegate()
let document = MyDocument()
document.delegate = delegate
// Compiled in Release mode, …Run Code Online (Sandbox Code Playgroud) weak-references reference-counting automatic-ref-counting swift
c++ ×3
delphi ×2
swift ×2
.net ×1
api ×1
constructor ×1
destructor ×1
interface ×1
iphone ×1
javascript ×1
objective-c ×1
oop ×1
release ×1
shared-ptr ×1
stl ×1
stlport ×1
string ×1
struct ×1
wwdc ×1