{"id":4833,"date":"2018-02-13T15:49:39","date_gmt":"2018-02-13T07:49:39","guid":{"rendered":"http:\/\/blog.qdac.cc\/?p=4833"},"modified":"2018-02-13T16:02:07","modified_gmt":"2018-02-13T08:02:07","slug":"%e6%95%99%e7%a8%8b-delphi-%e4%b8%ad%e7%9a%84%e8%87%aa%e5%8a%a8%e5%bc%95%e7%94%a8%e8%ae%a1%e6%95%b0%e4%bd%bf%e7%94%a8%e8%a7%84%e5%88%99","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=4833","title":{"rendered":"[\u6559\u7a0b] Delphi \u4e2d\u7684\u81ea\u52a8\u5f15\u7528\u8ba1\u6570\u4f7f\u7528\u89c4\u5219"},"content":{"rendered":"<div id=\"globalWrapper\">\n<div id=\"column-content\">\n<div id=\"content\">\n<div id=\"bodyContent\" class=\"mw-body\">\n<div id=\"mw-content-text\" class=\"mw-content-ltr\" dir=\"ltr\" lang=\"en\">\n<p>Delphi \u7528\u6237\u5e94\u8be5\u65e9\u5df2\u7ecf\u719f\u6089\u81ea\u52a8\u5f15\u7528\u8ba1\u6570\u7684\u6982\u5ff5\uff08ARC\uff09\uff0cDelphi \u00a0\u5f88\u65e9\u5c31\u5f00\u59cb\u652f\u6301\u63a5\u53e3\u3001\u52a8\u6001\u6570\u7ec4\u548c\u5b57\u7b26\u4e32\u7684\u81ea\u52a8\u5f15\u7528\u8ba1\u6570\uff08DCC32\u3001DCC64\u3001DCCOSX\uff09\uff0c\u79fb\u52a8\u7f16\u8bd1\u5668\u5219\u52a0\u5165\u5bf9\u7c7b\u7684\u81ea\u52a8\u5f15\u7528\u8ba1\u6570\u652f\u6301\uff0c\u56e0\u6b64\u8fd8\u52a0\u4e86\u5165\u5f31\u5f15\u7528\u7684\u6982\u5ff5\uff0c\u4ee5\u7ba1\u7406\u5faa\u73af\u5f15\u7528\u3002\u6709\u4e2a\u9884\u7f16\u8bd1\u6761\u4ef6\u6307\u4ee4\u00a0AUTOREFCOUNT \u53ef\u4ee5\u68c0\u6d4b\u5f53\u524d\u662f\u5426\u7f16\u8bd1\u5668\u662f\u5426\u542f\u7528\u4e86\u81ea\u52a8\u5f15\u7528\u8ba1\u6570\u3002<\/p>\n<p>\u5728\u542f\u7528\u81ea\u52a8\u5f15\u7528\u8ba1\u6570\u7684\u60c5\u51b5\u4e0b\uff0c\u7f16\u7801\u53d1\u751f\u4e86\u4ee5\u4e0b\u53d8\u5316\uff1a<\/p>\n<ul>\n<li>\u5bf9\u8c61\u521b\u5efa\u4e0d\u518d\u9700\u8981\u624b\u5de5\u91ca\u653e\uff0c\u6240\u4ee5\u4e0b\u9762\u7684\u4ee3\u7801\u662f\u6ca1\u6709\u5185\u5b58\u6cc4\u9732\u95ee\u9898\u7684\n<pre class=\"lang:delphi decode:true\">class procedure TMySimpleClass.CreateOnly;\r\nvar\r\n  MyObj: TMySimpleClass;\r\nbegin\r\n  MyObj := TMySimpleClass.Create;\r\n  MyObj.DoSomething;\r\nend;<\/pre>\n<\/li>\n<li>\u5faa\u73af\u5f15\u7528\u9700\u8981\u901a\u8fc7 weak \u6307\u793a\u7b26\uff0c\u5426\u5219\u4f1a\u9020\u6210\u5bf9\u8c61\u65e0\u6cd5\u91ca\u653e\u3002<\/li>\n<li>\u5982\u679c\u8981\u63d0\u524d\u91ca\u653e\uff0c\u4f7f\u7528 DisposeOf \u3002\u50cf\u4e0b\u9762\u76f4\u63a5\u8bbe\u7f6e\u4e3a nil \u53ea\u662f\u51cf\u5c11\u5f15\u7528\u8ba1\u6570\uff0c\u5982\u679c\u6ca1\u6709\u5176\u5b83\u5f15\u7528\uff0c\u4f1a\u76f4\u63a5\u91ca\u653e\u5bf9\u8c61\uff0c\u4f46\u5982\u679c\u6709\u5176\u5b83\u5f15\u7528\u5b58\u5728\uff0c\u5c31\u4e0d\u4f1a\u91ca\u653e\u3002\u6240\u4ee5\uff0c\u5728\u4f7f\u7528 FreeAndNil \u65f6\uff0c\u4e00\u5b9a\u8981\u6ce8\u610f\u8fd9\u4e00\u70b9\uff0c\u5728\u79fb\u52a8\u5e73\u53f0\uff0cFreeAndNil \u7b49\u4ef7\u4e8e\u8d4b\u503c\u4e3a NIL\u3002\n<pre class=\"lang:delphi decode:true\">class procedure TMySimpleClass.SetNil;\r\nvar\r\n  MyObj: TMySimpleClass;\r\nbegin\r\n  MyObj := TMySimpleClass.Create;\r\n  MyObj.DoSomething (False); \/\/ True =&gt; raise\r\n  MyObj := nil;\t\r\n  \/\/ do something else\r\nend;<\/pre>\n<\/li>\n<li>Free \u65b9\u6cd5\u4f9d\u7136\u53ef\u4ee5\u88ab\u8c03\u7528\uff0c\u53ea\u4e0d\u8fc7\u5b83\u5728\u81ea\u52a8\u5f15\u7528\u8ba1\u6570\u6a21\u5f0f\u4e0b\uff0c\u88ab\u7f16\u8bd1\u5668\u4f18\u5316\u4e3a X:=nil \u3002<\/li>\n<\/ul>\n<p>\u7efc\u4e0a\u8ff0\uff0c\u8981\u76f4\u63a5\u7acb\u5373\u91ca\u653e\u4e00\u4e2a\u5bf9\u8c61\uff0c\u90a3\u4e48\u5728\u81ea\u52a8\u5f15\u7528\u8ba1\u6570\u6a21\u5f0f\u4e0a\uff0c\u8c03\u7528 DisposeOf \uff0c\u8c03\u7528 Free \u6216 FreeAndNil \u4e0d\u80fd\u4fdd\u8bc1\u8fd9\u4e00\u70b9\u3002<\/p>\n<h4>\u3010\u6ce8\u610f\u3011<\/h4>\n<p>1\u3001DisposeOf \u662f\u5c06\u5bf9\u8c61\u7f6e\u4e8e\u4e00\u79cd\u7279\u6b8a\u7684\u201c\u50f5\u5c38\u201d\u72b6\u6001\uff0c\u5176\u6790\u6784\u51fd\u6570\u88ab\u8c03\u7528\uff0c\u4f46\u5bf9\u8c61\u672c\u8eab\u5360\u7528\u7684\u5185\u5b58\u7a7a\u95f4\u5e76\u6ca1\u6709\u88ab\u7acb\u5373\u56de\u6536\uff0c\u5176\u5360\u7528\u7684\u5185\u5b58\u53ea\u6709\u5728\u5176\u5f15\u7528\u8ba1\u6570\u88ab\u6e05\u4e3a 0 \u65f6\u624d\u4f1a\u76f4\u63a5\u7684\u91ca\u653e\u3002<\/p>\n<p>2\u3001Delphi \u7684\u5bf9\u8c61\u7684\u5f15\u7528\u8ba1\u6570\u6700\u5927\u4e3a 2<sup>30 \u00a0<\/sup>\u6b21\uff0c\u6709\u4e00\u4f4d\u88ab\u7528\u505a\u6807\u5fd7\u662f\u5426\u5df2\u7ecf\u8c03\u7528\u4e86 DisposseOf\u3002<\/p>\n<p>3\u3001Weak \u6ce8\u89e3\u4e0d\u9002\u5408\u7528\u4e8e\u5927\u91cf\u5bf9\u8c61\u5f15\u7528\u7684\u573a\u5408\uff0c\u5b83\u867d\u7136\u4e0d\u4f1a\u589e\u52a0\u5f15\u7528\u8ba1\u6570\uff0c\u4f46\u4f1a\u8c03\u7528\u5185\u90e8\u7684 RegisterWeakRef \u548c UnregisterRef\u00a0\u51fd\u6570\u6765\u5904\u7406\u5f31\u5f15\u7528\u4fe1\u606f\u3002\u8fd9\u4e9b\u989d\u5916\u7684\u4ee3\u7801\u5728\u521b\u5efa\u5927\u91cf\u5bf9\u8c61\u65f6\uff0c\u5b83\u5bf9\u6548\u7387\u7684\u5f71\u54cd\u8fd8\u662f\u8ba9\u4eba\u6709\u70b9\u7ea0\u7ed3\u7684\u3002\u5bf9\u4e8e\u8fd9\u79cd\u573a\u5408\uff0c\u63a8\u8350\u7528 Unsafe \u6ce8\u89e3\u6765\u4ee3\u66ff Weak\uff08\u867d\u7136\u5b98\u65b9\u4e0d\u592a\u63a8\u8350\uff09\u3002\u65e0\u8bba\u662f Weak \u8fd8\u662f Unsafe\uff0c\u5728\u4f7f\u7528\u65f6\u90fd\u8981\u6bd4\u8f83\u5c0f\u5fc3\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u6cc4\u9732\u3002Unsafe \u4e0e Weak \u7684\u7684\u4e0d\u540c\u5728\u4e8e weak \u5f15\u7528\u7684\u5bf9\u8c61\u91ca\u653e\u65f6\uff0c\u76f8\u5e94\u7684\u5f15\u7528\u4f1a\u88ab\u7f6e\u4e3a NIL\uff0c\u800c unsafe \u662f\u4e0d\u7ba1\u8fd9\u70b9\u4e8b\u7684\uff0c\u8fd9\u4e5f\u5c31\u89e3\u91ca\u4e86\u4e3a\u5565 weak \u6ce8\u89e3\u4f1a\u5f15\u53d1 RegisterWeakRef \/ UnregisterRef \u8c03\u7528\u4e86\u3002<\/p>\n<p>4\u3001\u5982\u679c\u4f60\u4e0d\u5e0c\u671b\u4f60\u7684\u5bf9\u8c61\u88ab\u81ea\u52a8\u7ba1\u7406\u5f15\u7528\u8ba1\u6570\uff0c\u53ef\u4ee5\u91cd\u65b0\u5b9e\u73b0 IInterface \u63a5\u53e3\u7684 __AddRef \u548c __Release \u65b9\u6cd5\uff0c\u5c31\u8c61 TComponent \u505a\u7684\u90a3\u6837\u3002<\/p>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Delphi \u7528\u6237\u5e94\u8be5\u65e9\u5df2\u7ecf\u719f\u6089\u81ea\u52a8\u5f15\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"","ast-site-content-layout":"default","site-content-style":"default","site-sidebar-style":"default","ast-global-header-display":"","ast-banner-title-visibility":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","ast-disable-related-posts":"","theme-transparent-header-meta":"","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","astra-migrate-meta-layouts":"default","ast-page-background-enabled":"default","ast-page-background-meta":{"desktop":{"background-color":"var(--ast-global-color-4)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"ast-content-background-meta":{"desktop":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"tablet":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""},"mobile":{"background-color":"var(--ast-global-color-5)","background-image":"","background-repeat":"repeat","background-position":"center center","background-size":"auto","background-attachment":"scroll","background-type":"","background-media":"","overlay-type":"","overlay-color":"","overlay-opacity":"","overlay-gradient":""}},"footnotes":""},"categories":[8,603],"tags":[747,450,749,748],"class_list":["post-4833","post","type-post","status-publish","format-standard","hentry","category-delphi","category-603","tag-arc","tag-delphi","tag-unsafe","tag-weak"],"views":4932,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/4833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4833"}],"version-history":[{"count":4,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/4833\/revisions"}],"predecessor-version":[{"id":4837,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/4833\/revisions\/4837"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}