`
RednaxelaFX
  • 浏览: 3016409 次
  • 性别: Icon_minigender_1
  • 来自: 海外
社区版块
存档分类
最新评论

this的寿命?

阅读更多
NyaRuRuさん的blog真是一个宝库,有许多有启发性的日记。刚才读到这么一篇:this の寿命,虽然是06年的老文,却还是让我惊讶了一番。

文中提到,“this”在实际运行中并不一定在其作用域内都存活;在作用域结束前,它有可能就已经被回收(delete this)了。文中举了这样一个例子(稍微修改过):
testGC.cs:
// compile this file with the following command:
// csc /o /d:TRACE testGC.cs

using System;
using System.Threading;
using System.Diagnostics;

class Test {
    IntPtr _myHandle = IntPtr.Zero;

    ~Test( ) {
        Trace.WriteLine( "Finalize; Release Handle" );
    }

    public void Foo( ) {
        Trace.WriteLine( "Get Handle from OS : 123456" );
        _myHandle = ( IntPtr ) 123456;
        Bar( _myHandle );
    }

    public void Bar( IntPtr handle ) {
        GC.Collect( );
        Thread.Sleep( 1000 );
        Trace.WriteLine( "Use Handle : " + handle.ToString( ) );
    }
}

static class Program {
    static void Main( string[ ] args ) {
        Trace.Listeners.Add( new ConsoleTraceListener( ) );
        new Test( ).Foo( );
    }
}

运行的结果是:
引用
Get Handle from OS : 123456
Finalize; Release Handle
Use Handle : 123456

从trace结果可以看到,Test的析构器在Test.Foo()还在运行中就执行了。这与一般认识似乎相差甚远:Main()里对Test.Foo()的调用尚未完成,也就是说应该还有对这个对象的引用,所以这个Test对象应该暂时还无法被析构才对。
事实上,受到JIT的内联优化等影响,程序运行的时候还有没有对某个对象的引用,与源码中不一定总是对应的;极端情况下,在一个对象的构造器执行过程中,这个对象就可能已经被析构了。在这帖有一串有趣的讨论。

Chris Brumme也写过一篇关于GC.KeepAlive()的帖子:Lifetime, GC.KeepAlive, handle recycling
分享到:
评论
3 楼 hd700 2008-10-07  
我也认为是jit的问题,我曾经写了一篇关于局部变量被回收的问题。http://blog.csdn.net/fuadam/archive/2008/09/03/2876002.aspx
2 楼 RednaxelaFX 2008-10-04  
COM的话多半跟这个没什么关系……这个是JIT带来的问题,COM不使用JIT,GC也不是M&S而是RC的。
1 楼 lwwin 2008-10-04  
COM里面似乎也会碰到……不知道是不是类似的?
memberfunc里面delete this…………

相关推荐

    脉冲阈值法测注入式激光器的载流子寿命

    This paper describes the measurement of carrier lifetime of GaAs/GaAlAs DH lasers. Simplified experimental system without high speed detecter and very broad band amplifier is used.

    学生成绩管理系统

    public class Student implements Serializable{// 对象的寿命通常随着生成该对象的程序的终止而终止。有时候,可能需要将对象的状态保存下来,在需要时再将对象恢复。我们把对象的这种能记录自己的状态以便将来再生...

    详解linux电源管理驱动编写

    对于嵌入式设备来说,合适的电源管理,不仅可以延长电池的寿命,而且可以省电,延长设备运行时间,在提高用户体验方面有很大的好处。所以,各个soc厂家在这方面花了很多的功夫。下面,我们可以看看linux是如何处理...

    VF企业工资管理系统(系统+课程设计)

    例如:检索迅速、查找方便、可靠性高、存储量大、保密性好、寿命长、成本低等。这些优点能够极大地提高工资管理的效率,也是科学化、正规化管理的重要条件。 设计目的 对于企、事业单位的工资发放来说,不需要太大型的...

    PHP串行化与反串行化实例分析

    对象也是一种在内存中存储的数据类型,他的寿命通常随着生成该对象的程序的终止而终止。有时候可能需要把对象的状态保存下来,需要时再将其回复。串行化是把每个对象转化为二进制字符串。 <?php class Person { ...

    基于单片机的智能充电器.doc

    在生活中更好的维护了充电电池, 延长了它的使用寿命。 关键词:充电器;单片机;锂电池;MAX1898 Lithium Battery Charger Design Based On Single Chip Abstract Electronic technology's fast development causes...

    TimeLivedApplication:这是一个 Android 应用程序,可以显示您活了多少天

    实时应用程序这是一个 Android 应用程序,可以计算...you may not use this file except in compliance with the License.You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0Unless

    基于单片机的液位控制系统设计.doc

    基于单片机的液位测量装置具有测量准确、重复性好、功耗低、使用寿命长的特点, 是广泛采用的技术。在深入学习科学发展观的同时,电子设备的设计也需融入可持续发 展的设计理念。故此,在基于单片机的液位测量装置...

    2015基于单片机的液位控制系统设计.doc

    基于单片机的液位测量装置具有测量准确、重复性好、功耗低、使用寿命长的特点, 是广泛采用的技术。在深入学习科学发展观的同时,电子设备的设计也需融入可持续发 展的设计理念。故此,在基于单片机的液位测量装置...

    基于PLC的低压断路器型式试验的系统设计

    Abstract: The automatic control that this lesson a pattern for is to breaking road machine experiment proceeds the design, choosing to use FX2N — 64MRa type PLC as the programmable preface controller...

    vue-snarkdown:Vue组件包装了小巧的Markdown解析器Snarkdown

    < template> < Snarkdown> _this_ is **easy** to `use`. </ Snarkdown></ template>< script >import Snarkdown from '@swayable/vue-snarkdown'export default { components : { Snarkdown , } ,}</ ...

    数据分析英语怎么说.docx

    Let me make this clear: A bar chart is not analytics. 首先我们必须明白: 一张条形图不是数据分析. 5. Good skills of data analysis and reporting by computer. 熟练运用办公自动化进行数据分析和报告. 6. ...

    droneflightgraphs

    无人机飞行产生的原始数据被转换成各种图形,以优化诸如电池电压和电池寿命之类的功能,从而最大程度地提高了Python无人机飞行的成功率。 目录 [关于此项目](#about-this-project) [内置](#内置) [设置](#...

    matlab轮询调度算法代码-rt-ifttt:“RT-IFTTT:具有触发条件感知的灵活轮询间隔的实时物联网框架”(RTSS2017)中使用

    由于用户希望小程序在一定的延迟内响应更改后的传感器值,而传感器通常电池电量有限,因此在正确的时间点读取传感器值对于IoT框架在最大程度地延长电池寿命的同时支持小程序的实时响应至关重要传感器。 但是,现有的...

    电子设计大赛设计报告.doc

    关键字: MSP430F169单片机 E6A2-CWZ3C编码器 PID算法 H桥电机驱动 Abstract: this system by TI company MSP430F169 microcontroller as the control core of the whole system, the E6A2 - CWZ3C encoder as the ...

    Theoretical calculation of energy levels and radiative lifetimes of Tl I

    Furthermore, the radiative lifetimes of this five series are also calculated. The calculated values of energy levels and lifetimes are in good agreement with the experimental results.

    一种新型直流电源监控系统的设计

    Abstract:The unattended DC power supervise control system are introduced in this article,and the module of comprehensive monitoring、the module of battery inspection and the module of insulation ...

    网上购物系统

    This System use jdk1.6.0_03 + sun_java_wireless_toolkit-2_5_2- + eclipse-SDK-3.0 + eclipseme.feature_1.7.7 this Develops platform to develop and test.Design software which used flow chart and so on, ...

    Simulation of quantum-well slipping effect on optical bandwidth in transistor laser

    An optical bandwidth analysis of a quantum-well (16 nm) transistor laser with ... No remarkable resonance peak, limiting factor in laser diodes, is observed during this modification in transistor laser

    嵌入式红绿灯控制系统

    固体冷光源,环氧树脂封装,灯体内也没有松动的部分,不存在灯丝发光易烧、热沉积、光衰快等缺点,使用寿命可达5 万到10 万小时,比传统光源寿命长10 倍以上。 LED 利环保:LED 是一种绿色光源,环保效益更佳。...

Global site tag (gtag.js) - Google Analytics