{"id":5688,"date":"2023-07-24T10:09:30","date_gmt":"2023-07-24T02:09:30","guid":{"rendered":"https:\/\/blog.qdac.cc\/?p=5688"},"modified":"2023-07-24T13:30:53","modified_gmt":"2023-07-24T05:30:53","slug":"%e6%95%99%e7%a8%8bvirtualtreeview-%e7%bb%84%e4%bb%b6%e4%b8%ad%ef%bc%8c%e9%87%8a%e6%94%be%e6%97%b6%e5%8f%af%e8%83%bd%e9%80%a0%e6%88%90%e5%81%87%e6%ad%bb%e9%97%ae%e9%a2%98%e4%bf%ae%e5%a4%8d","status":"publish","type":"post","link":"https:\/\/blog.qdac.cc\/?p=5688","title":{"rendered":"[\u6559\u7a0b]VirtualTreeView \u7ec4\u4ef6\u4e2d\uff0c\u91ca\u653e\u65f6\u53ef\u80fd\u9020\u6210\u5047\u6b7b\u95ee\u9898\u4fee\u590d"},"content":{"rendered":"\n<p>\u5728 TBaseVirtualTree.Destroy \u51fd\u6570\u91cc\uff0c\u6709\u4e00\u6bb5\u4ee3\u7801\u662f\u8fd9\u4e48\u5199\u7684\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if WasValidating then\n  begin\n    \/\/ Make sure we dequeue the two synchronized calls from ChangeTreeStatesAsync(), fixes mem leak and AV reported in issue #1001, but is more a workaround.\n    while CheckSynchronize()  do\n      begin\n      Sleep(1);\n      end;\n  end;\/\/ if<\/code><\/pre>\n\n\n\n<p>\u6ce8\u610f\u8fd9\u4e2a while \u5faa\u73af\uff0c\u56e0\u4e3a\u5b83\u8c03\u7528\u7684 CheckSynchronize \u53bb\u68c0\u67e5\u540e\u53f0\u7ebf\u7a0b\u662f\u4e0d\u662f\u5f02\u6b65\u8c03\u7528\u4e86\u81ea\u5df1\uff0c\u4f46\u5b83\u6ca1\u6709\u8003\u8651\u5982\u679c\u6709\u5176\u5b83\u7ebf\u7a0b\u6216\u8005\u540c\u6b65\u4ee3\u7801\u5728\u9891\u7e41 TThread.Synchronize \u6216  TThread.Queue\/TThread.ForceQueue \u65f6\u4f1a\u4ea7\u751f\u4ec0\u4e48\u5f71\u54cd\uff0c\u5982\u679c\u4e00\u65e6\u53d1\u751f\u8fd9\u79cd\u60c5\u51b5\uff0c\u5e94\u7528\u5c31\u53ef\u80fd\u4f1a\u957f\u65f6\u95f4\u963b\u585e\u5728\u8fd9\u5757\uff0c\u65e0\u6cd5\u6b63\u5e38\u54cd\u5e94\uff0c\u9020\u6210\u5047\u6b7b\u73b0\u8c61\u3002<\/p>\n\n\n\n<p>\u5bf9\u4e8e\u8fd9\u4e00\u4e2a\u95ee\u9898\u7684\u4fee\u590d\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u8c03\u7528\u8ba1\u6570\u7684\u65b9\u5f0f\u4e88\u4ee5\u89e3\u51b3\uff1a<\/p>\n\n\n\n<p>1\u3001\u6dfb\u52a0\u4e00\u4e2a\u6574\u5f62\u53d8\u91cf FPendingSyncProcs\uff0c\u6765\u8bb0\u5f55\u672c\u8eab\u8c03\u7528 TThread.Synchronize  \u7684\u6b21\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>private\n    FPendingSyncProcs:Integer;\/\/swish:\u8bb0\u5f55\u4e0b\u6b63\u5728\u8fdb\u884c\u7684\u5f02\u6b65\u8c03\u7528\uff0c\u4ee5\u4fdd\u8bc1\u9000\u51fa\u65f6\u76f8\u5173\u8c03\u7528\u5df2\u7ecf\u7ed3\u675f<\/code><\/pre>\n\n\n\n<p>2\u3001\u6bcf\u6b21\u540c\u6b65\u51fd\u6570\u5904\u7406\u65f6\uff0c\u51cf\u5c11\u8fd9\u4e00\u8ba1\u6570<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>procedure TBaseVirtualTree.ChangeTreeStatesAsync(EnterStates, LeaveStates: TVirtualTreeStates);\nbegin\n  \/\/TODO: If this works reliable, move to TWorkerThread\n  if not (csDestroying in ComponentState) then\n    begin\n    \/\/swish:Increment invoke refs\n    AtomicIncrement(FPendingSyncProcs);\n    TThread.Synchronize(nil, procedure\n      begin\n        \/\/Decrement invoke refs\n        AtomicDecrement(FPendingSyncProcs);\n        \/\/ Prevent invalid combination tsUseCache + tsValidationNeeded (#915)\n        if not ((tsUseCache in EnterStates) and (tsValidationNeeded in FStates + LeaveStates)) then\n          DoStateChange(EnterStates, LeaveStates);\n        if (tsValidating in FStates) and (tsValidating in LeaveStates) then\n          UpdateEditBounds();\n      end);\n    end;\nend;\n\nprocedure TBaseVirtualTree.MeasureItemHeight(const Canvas: TCanvas; Node: PVirtualNode);\n\n\/\/ If the height of the given node has not yet been measured then do it now.\n\nvar\n  NewNodeHeight: TDimension;\n\nbegin\n  if not (vsHeightMeasured in Node.States) then\n  begin\n    Include(Node.States, vsHeightMeasured);\n    if (toVariableNodeHeight in FOptions.MiscOptions) then\n    begin\n      NewNodeHeight := Node.NodeHeight;\n      \/\/ Anonymous methods help to make this thread safe easily.\n      if (MainThreadId &lt;&gt; GetCurrentThreadId) then\n        begin\n        \/\/swish:Increment invoke refs\n        AtomicIncrement(FPendingSyncProcs);\n        TThread.Synchronize(nil,\n          procedure\n          begin\n            \/\/swish:Decrement invoke refs\n            AtomicDecrement(FPendingSyncProcs);\n            DoMeasureItem(Canvas, Node, NewNodeHeight);\n            SetNodeHeight(Node, NewNodeHeight);\n          end\n        )\n        end\n      else\n      begin\n        DoMeasureItem(Canvas, Node, NewNodeHeight);\n        SetNodeHeight(Node, NewNodeHeight);\n      end;\n    end;\n  end;\nend;<\/code><\/pre>\n\n\n\n<p>3\u3001\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u68c0\u67e5\u8fd9\u4e2a\u8ba1\u6570\u662f\u5426\u4e3a0\uff0c\u5982\u679c\u4e3a0\uff0c\u5219\u4e0d\u518d\u5faa\u73af\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>if WasValidating then\n  begin\n    \/\/ Make sure we dequeue the two synchronized calls from ChangeTreeStatesAsync(), fixes mem leak and AV reported in issue #1001, but is more a workaround.\n    \/\/swish:Add FPendingSyncProcs reference check avoid dead loop\n    while CheckSynchronize() and (FPendingSyncProcs>0) do\n      begin\n      Sleep(1);\n      end;\n  end;\/\/ if<\/code><\/pre>\n\n\n\n<p>\u81f3\u6b64\uff0c\u95ee\u9898\u89e3\u51b3\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 TBaseVirtualTree.D [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","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":[739,813,330],"class_list":["post-5688","post","type-post","status-publish","format-standard","hentry","category-delphi","category-603","tag-virtualtreeview","tag-813","tag-330"],"views":1998,"_links":{"self":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5688","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=5688"}],"version-history":[{"count":3,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5688\/revisions"}],"predecessor-version":[{"id":5691,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=\/wp\/v2\/posts\/5688\/revisions\/5691"}],"wp:attachment":[{"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5688"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5688"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.qdac.cc\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5688"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}