投稿数 - 437, コメント - 41373, トラックバック - 156

誰も参照していないフォームは、何故 GC の対象にならないのか

ネタ元 http://www.atmarkit.co.jp/bbs/phpBB/viewtopic.php?topic=28778&forum=7&5

こんな話は大好きだ。

確かに、ローカルスコープで作ったインスタンスは、ローカル変数しか参照していなく、他に誰も参照していなかったら、いつか GC の対象になる。Form とて例外ではなかろう。しかし、実際はそうではない。Form は明確に誰も参照していなくても、GC の対象とならないのだ。(後で述べるが、実際は参照している奴が存在する)

Form に限らずコントロールは全てウィンドウハンドルを持つ。コントロールは全て「ウィンドウ」なのだ。.NET ではウィンドウハンドルを持つウィンドウを「System.Windows.Forms.Control」として扱っている。各コントロールはこのクラスから派生しているわけだ。Form もその一つである。Control は内部に System.Windows.Forms.NativeWindow のインスタンスを持ち、ウィンドウハンドルを管理している。Control は Dispose() 時に NativeWindow.DestroyHandle() を呼んでウィンドウハンドルを破棄しているはずだ。

NativeWindow のヘルプを見ると「ウィンドウは、ウィンドウ ハンドルが関連付けられている場合は、ガベージ コレクションの対象にはなりません。」とある。何故だ?どう実現している?

詳細に調べていくと、NativeWindow は内部に GCHandle のインスタンスを保持していた。NativeWindow がウィンドウハンドルに関連付けられていると GC の対象にならないのは GCHandle が鍵なのだ。

GCHandle.Alloc() で新しい GCHandle を作成する。そして GCHandle.Target にガベージコレクションの対象としたくないインスタンスの参照を設定してやればよいのだ。今回の例で言えば、トップレベルの Form のインスタンスの参照だ。(ここでトップレベルの Form のインスタンスの参照をどのように得ているのかは調査中)

結局、Form は自身が内部に持つ GCHandle に参照される事になる。後は GCHandle が誰かに参照されれば問題は解決だ。

GCHandle は誰に参照されているのか?

これは CLR しか預かり知らぬ所。CLR が管理している領域でもあるのか、GC が管理しているのかは分からない。(情報求ム)とにかく、GCHandle は GCHandle.Alloc() したら誰かに参照されている状態で取得できる。

改めて .NET の設計の巧さに拍手だ。

ウィンドウハンドルに限らず、GCHandle を使えば GC を上手に使いこなす事ができるだろう。

投稿日時 : 2006年2月27日 14:35

フィードバック

# re: 誰も参照していないフォームは、何故 GC の対象にならないのか

やっぱり、囚人さんが釣れると思ったw
2006/02/27 15:02 | じゃんぬ

# re: 誰も参照していないフォームは、何故 GC の対象にならないのか

もう好みがバレているようですね。
2006/02/27 15:16 | 囚人

# re: 誰も参照していないフォームは、何故 GC の対象にならないのか

誘う時にバレているって言ってませんでしたっけ? (w
こうやって Blog で掘り下げて頂くと助かりますね。
2006/02/27 16:02 | じゃんぬ

# re: 誰も参照していないフォームは、何故 GC の対象にならないのか

GC だけに注目しているようなので、ネタ投下 ;-)

モーダレスなフォームである Form1, Form2 が存在し、Form1 上に TextBox1 が配置されている時

TextBox textBox = TextBox1;

Form1.Controls.Remove (textBox);
Form2.Controls.Add (textBox);

が「ちゃんと動く」のは何故でしょうか?

特に、Remove() と Add() の行間に注目です。

Windows には「子ウィンドウ(=コモンコントロールは出来合いの子ウィンドウです)は必ずトップレベルウィンドウに保有されていなければならない」という大原則があるはずなのですが。。。
2006/03/02 11:57 | 渋木宏明(ひどり)

# re: 誰も参照していないフォームは、何故 GC の対象にならないのか

>GC だけに注目しているようなので、ネタ投下 ;-)

む・・・。これは一体…。
ネタ爆弾被弾しました。次回作にご期待ください^^;
2006/03/03 8:52 | 囚人

# 誰も参照していないフォームは、何故 GC の対象にならないのか(その2)

誰も参照していないフォームは、何故 GC の対象にならないのか(その2)
2006/04/10 2:08 | 囚人のジレンマな日々

# thanks for the postmishertAtroro

Useful blog website, keep me personally through searching it, I am seriously interested to find out another recommendation of it.
2010/11/08 22:30 | emt training

# pastillas precio mexico

Cuanto cuesta en chile http://farmacia-en-linea.life/eliprim.html sin receta en Argentina.
2018/01/07 10:53 | farmacia-en-linea

# Where to order cheap

Buy generic online usa http://www.hillsidechamber.com:88/UserProfile/tabid/90/UserID/536906/Default.aspx generic over the counter.
2018/01/08 6:43 | to buy in toronto

# Can I buy online uk

Generic lowest price http://www.diroo.com.ar/user/profile/8840 Get online uk.
2018/01/08 15:03 | cost of in usa

# Can I buy in new zealand

purchasing in dubai http://www.ragusassociates.com/?option=com_k2&view=itemlist&task=user&id=66258 buying tablets.
2018/01/08 22:27 | Online canada overnight

# Generic price in usa

cheapest uk http://www.micobiota.cl/?option=com_k2&view=itemlist&task=user&id=39772 best price for.
2018/01/09 5:42 | can you buy online in canada

# Generique sanofi

Generique maroc http://medicamentsonline.life/janumet.html prix France.
2018/01/10 1:14 | MEDICAMENTS ONLINE

# online kopen apotheek

Koop online, http://onlinemedicijnenbestellen.life/ventolin.html , veilig internet.
2018/01/10 3:41 | Medicijnen Bestellen Online

# kosta stockholm

sweden göteborg, http://apotekvarerpanettet.life/tretinoin.html , kapslar billiga.
2018/01/12 1:36 | Apotekvarer Pa Nettet

# priser apoteket priser

snabb leverans göteborg, http://apotekvarerpanettet.life/promethazine.html , köpa billigt göteborg.
2018/01/12 9:00 | apotekvarer-pa-nettet life

# online Norge pris

bestille Sverige, http://apoteknettbutikknorge.life/silvitra.html , kjøpe i Sverige pris.
2018/01/12 12:49 | apoteknettbutikknorge life

# természetes eladó

eladási, http://gyogyszertar-online-hu.life/coumadin.html - Gyógyszertár ára.
2018/01/12 13:27 | Gyogyszertar Hungaria Online

# alternatief belgie

Apotheek belgie online, http://onlineapothekerzonderrecept.life/buspar.html , kopen via internet zonder recept te koop.

# megvesz magyarországon

Alacsony árak, http://gyogyszertaronlinehu.life/propranolol.html - természetes budapest.
2018/01/12 18:37 | Online Gyogyszertar Hungaria

# kosten prijzen

Bestellen paypal recept, http://onlineapothekernederland.life/aldactone.html , kopen zonder recept nederland online.
2018/01/13 1:46 | Online Apotheker Nederland

# generiska alternativ till pris

bestalla online, http://apotekvarerpanettet.life/slimex.html , köpa Sverige recept.
2018/01/13 3:39 | Apotekvarer Pa Nett

# kjøp bergen

apotek Norge København, http://apotek-norway.life/aldactone.html , Generisk Gøteborg.
2018/01/13 4:43 | apotek-norway.life

# köpa sverige danmark

Sverige apotek online, http://apotekvarerpanettet.life/arimidex.html , online recept.
2018/01/13 11:08 | Apotekvarer Pa Nett

# kan man kjøpe uten resept i Norge pris

pris apotek Sverige Danmark, http://apoteknorway.life/precose.html , salg Norge.
2018/01/13 11:46 | Apotek Norway

# eladás eladási árfolyamok

Eladó budapest olcsón, http://online-gyogyszertar.life/hydrochlorothiazide.html - természetes budapest.
2018/01/13 12:26 | Gyogyszertar Online Hungaria

# bästa pris i sverige

bestall köpa, http://apotekvarerpanettet.life/keppra.html , kostar Stockholm.
2018/01/13 17:42 | Sverige Apotek Pa Natet

# billig jeg

beste sted å kjøpe jeg, http://apotek-norge-online.life/synthroid.html , For salg Oslo.
2018/01/13 21:57 | Apotek Online Norge

# kostnaden Gøteborg

bestille bergen, http://apoteknorgeonline.life/trazodone.html , Hvordan kjøpe København.
2018/01/14 6:39 | apoteknorgeonline

# köp pa natet online

malmö billigt, http://apotekvarerpanettet.life/slimex.html , kapslar kostnad.
2018/01/14 7:23 | sverige-apotek-pa-natet life

# generieke bestellen paypal

Kopen apotheek nederland online, http://internetapotheek-nl.life/amoxil.html , kopen zonder recept in nederland belgie.
2018/01/14 13:02 | internet apotheek nl life

# billiga sverige

receptfria online, http://apotekvarerpanettet.life/periactin.html , generisk billigt.
2018/01/14 17:29 | Apotek Pa Natet

# Over the counter date

can i take without a doctor prescription http://www.andersfray.com/blog/?page_name=zyloprim average cost of prescription.
2018/01/14 23:41 | andersfray.com

# Best non prescription

where can i order http://www.jimsbigthings.com/?page_name=anafranil cheapest new zealand.
2018/01/16 3:50 | JIMSBIGTHINGS.COM

# españa generico

Precio en farmacias http://www.gwsisecurity.com/fig/data/farmacia/raloxifene.html onde comprar barato.
2018/01/22 10:02 | GWSISECURITY.COM

# Tabletten ohne rezept bestellen

Preise apotheke osterreich http://blakemarymor.com/DROPBOX/apotheke/ethinyl-estradiol-cyproterone.html pillen online kaufen.
2018/01/22 15:55 | BLAKE

# necesita receta mexico

Precio mas barato http://handledesigns.com/img/farmacia/epamin.html donde puedo comprar sin receta en estados unidos.
2018/01/22 16:14 | handle

# sin receta en argentina

Venta en farmacias ecuador http://highdesertpintohorse.org/machform/data/form_10940/farmacia/fluamar.html receta para.
2018/01/22 22:14 | HIGHDESERTPINTOHORSE.ORG

# Ohne rezepte in apotheke

apotheke preisvergleich http://carlsbadridersco.com/shop/media/apotheke/ogastoro.html online kaufen ohne rezept schweiz.
2018/01/23 0:32 | CARLSBADRIDERSCO

# generico españa farmacia

Donde comprar en tegucigalpa http://www.cypressassistance.org/wp-content/uploads/2017/04/farmacia/lanzap.html donde conseguir en Venezuela.
2018/01/23 3:51 | Cypress

# precio farmacia españa

Sin receta chile http://www.harrison1966.com/gallery/farmacia/flutiamik.html conseguir.
2018/01/23 3:51 | HARRISON CLASS OF 1966

# Online kaufen per paypal

billig kaufen per uberweisung http://chronovalve.com/ezg_data/apotheke/mexalen.html kaufen apotheke preis.
2018/01/23 6:26 | CHRONOVALVE.COM

# Tabletten dosis

preis wien http://danazheng.com/assets/fonts/apotheke/finahair.html online kaufen ohne rezept paypal.
2018/01/23 9:25 | DANAZHENG.COM

# Once preis

preis apotheke osterreich http://skansailclub.com/sites/default/files/color/apotheke/sildenafil.html Online bestellen belgie.
2018/01/23 11:36 | skaneateles

# comprar sin receta contrareembolso

Cuanto cuestan las pastillas en el salvador http://darkagemedia.com/DDD/farmacia/triamterene-hydrochlorothiazide.html España generico.
2018/01/23 13:39 | DarkAge Media

# Apotheke schweiz preis

zetpillen bestellen http://dungeontraining.net/images/thumb/apotheke/astonin-h.html pille ohne rezept bestellen.
2018/01/23 16:29 | dungeon training

# necesito receta para comprar en argentina

Compra generico en España http://alejandromarmol.com/blog/farmacia/cyflox.html compra Chile.
2018/01/23 18:36 | alejandromarmol.com

# mais barato

Venden el sin receta medica http://www.jessica-straus.com/system/expressionengine/cache/farmacia/paroxat.html donde puedo comprar en USA.
2018/01/23 19:55 | jessica-straus

# Deutschland erlaubt

deutschland http://golfscorecard.net/slideshowpro/apotheke/miranova.html tabletten koortslip.
2018/01/23 22:55 | golf scorecard

# donde comprar barata en españa

Compra venezuela http://www.metalnex.com/wp/design/farmacia/minox.html venta online España.
2018/01/24 0:47 | www.metalnex.com

# por internet contrareembolso

Comprar por internet España http://www.dimitriskyriakidis.com/wsf20img/wsf20img_FREE/wizard/farmacia/sporidex.html donde comprar sin receta en Chile.
2018/01/24 2:05 | www.dimitriskyriakidis.com

# compra online

Comprar en mexico http://www.banglaunited.com/farmacia/vermofree.html cuanto cuesta.
2018/01/24 6:41 | BANGLAUNITED.COM

# donde puedo comprar mexico

Precios argentina http://www.ukiahaviation.com/files/farmacia/detebencil.html pastillas como tomarlas.
2018/01/24 7:51 | ukiahaviation.com

# Consta preisvergleich

Tabletten pferd http://www.gocrossroads.net/oldsite/cgi-bin/bk/active_guestbook_backups/apotheke/spirolang.html kosten consta.
2018/01/24 12:09 | CROSSROADS CHURCH

# Rezeptfrei in deutschland

Losung kosten http://tepoztlanvacationrentals.com/llcj/cache/apotheke/ergotamin.html Kleine tabletten.
2018/01/24 12:56 | tepoztlanvacationrentals

# How to buy online uk

tablets price in south africa http://medications-online.men/ Can you buy over the counter in australia.
2018/02/15 0:33 | Medications

# Pill price in mumbai

cheap online overnight shipping http://medicationsonline.men prices uk.
2018/02/15 20:45 | MEDICATIONS ONLINE

# extended background check iowa state police

Prescreening, http://background-checking.science/employment-background-check-form-texas-dps.html private investigator schools.
2018/03/27 0:26 | background-checking science

# affordable background checks california

Jackson county missouri warrant check, http://arrest-lookup.science/criminal-record-search-in-texas.html ssa employment history.
2018/03/27 2:26 | lookup arrest

# safe background check uk free

Benton county arkansas court records online, http://background-check-yourself.science/free-dot-background-checks-online-no-credit-card-needed.html clark county nevada public property records.

# pre employment screening hove

Are death certificates public records in texas, http://advanced-background-checks.science/background-of-crime-commissioner.html quitman texas public records.

# how to check my background for free number

Warrant search queens ny, http://background-check-usa.science/fayette-county-traffic-court-docket-lexington-ky.html home sales public records.
2018/03/28 7:12 | background-check-usa.science

# tenant background check employment california criminal

Austin tx criminal records search, http://background-check-tenants.science/cost-of-background-check-questionnaires.html florida judicial case search.
2018/03/28 12:01 | background-check-tenants

# public police records kansas

Warrant search nevada, http://advance-background-checks.science/totally-free-criminal-records-background-checks.html do employment background checks show arrests.
2018/03/28 20:16 | checks background

# fbi criminal background check visa

Companies check, http://background-check-tenant.science/background-check-minnesota-nj-state-police-employment.html public police records san diego.
2018/03/29 2:29 | check background

# duval county fl public arrest records

Obtaining adoption records, http://background-check-someone.science/check-personal-background-galileo-galilei.html arkansas county court records online.
2018/03/29 10:59 | background-check-someone

# complete background investigation dhs

How to get court records in missouri, http://accurate-background-screening.science/public-records-information-mn-free-search.html california superior court divorce records.
2018/03/29 14:55 | screening background

# minnesota public records website

Free nationwide criminal background checks, http://accurate-background-checks.science/date-background-check-law-in-texas-7-year-criminal.html state of alaska criminal background check.
2018/03/29 21:56 | background checks

# federal criminal history background check in nj

Florida criminal records public search, http://background-check-solutions.science/public-records-for-el-paso-county-colorado.html criminal record check manitoba free.

# how to check background for free history background check

Search public records worcester ma, http://accurate-background-check.science/church-background-checks-volunteers.html hpd warrant check.

# what is a comprehensive background check reference check take

Police records california, http://background-check-sites.science/background-check-questions-for-employment-what-show-up-on-mypay.html court records duval county fl.
2018/03/30 8:40 | background check sites science

# what shows on background check do most employers use

Delaware county new york public records, http://access-background-checks.science/when-do-companies-do-background-check-job-offer.html inmate search by name.
2018/03/30 8:57 | checks background

# idaho criminal background check

How to check tenant credit, http://access-background-checks.stream hernando county arrest records.
2018/04/01 22:39 | access-background-checks

# jackson county florida public records search

Instant background check florida, http://advanced-background-check.science/medical-background-check-years-back.html tulsa court records.
2018/04/03 22:27 | access-background-checks

# re: ????????????????? GC ??????????

is chloroquine an antibiotic https://chloroquineorigin.com/# what is hydroxychloroquine sulfate
2021/08/09 5:26 | hydroxycholorquine

# My brother suggested I might like this blog. He used to be entirely right. This submit actually made my day. You can not consider simply how so much time I had spent for this info! Thanks!

My brother suggested I might like this blog.
He used to be entirely right. This submit actually made my day.
You can not consider simply how so much time I had spent for this info!
Thanks!

# My brother suggested I might like this blog. He used to be entirely right. This submit actually made my day. You can not consider simply how so much time I had spent for this info! Thanks!

My brother suggested I might like this blog.
He used to be entirely right. This submit actually made my day.
You can not consider simply how so much time I had spent for this info!
Thanks!

# My brother suggested I might like this blog. He used to be entirely right. This submit actually made my day. You can not consider simply how so much time I had spent for this info! Thanks!

My brother suggested I might like this blog.
He used to be entirely right. This submit actually made my day.
You can not consider simply how so much time I had spent for this info!
Thanks!

# My brother suggested I might like this blog. He used to be entirely right. This submit actually made my day. You can not consider simply how so much time I had spent for this info! Thanks!

My brother suggested I might like this blog.
He used to be entirely right. This submit actually made my day.
You can not consider simply how so much time I had spent for this info!
Thanks!

# hello!,I really like your writing very a lot! percentage we be in contact more about your post on AOL? I need an expert on this area to solve my problem. May be that's you! Taking a look ahead to peer you.

hello!,I really like your writing very a lot!
percentage we be in contact more about your post on AOL?
I need an expert on this area to solve my problem.
May be that's you! Taking a look ahead to peer you.

# hello!,I really like your writing very a lot! percentage we be in contact more about your post on AOL? I need an expert on this area to solve my problem. May be that's you! Taking a look ahead to peer you.

hello!,I really like your writing very a lot!
percentage we be in contact more about your post on AOL?
I need an expert on this area to solve my problem.
May be that's you! Taking a look ahead to peer you.

# hello!,I really like your writing very a lot! percentage we be in contact more about your post on AOL? I need an expert on this area to solve my problem. May be that's you! Taking a look ahead to peer you.

hello!,I really like your writing very a lot!
percentage we be in contact more about your post on AOL?
I need an expert on this area to solve my problem.
May be that's you! Taking a look ahead to peer you.

# hello!,I really like your writing very a lot! percentage we be in contact more about your post on AOL? I need an expert on this area to solve my problem. May be that's you! Taking a look ahead to peer you.

hello!,I really like your writing very a lot!
percentage we be in contact more about your post on AOL?
I need an expert on this area to solve my problem.
May be that's you! Taking a look ahead to peer you.

# Wow, marvelous weblog structure! How lengthy have you ever been blogging for? you made blogging look easy. The whole glance of your website is fantastic, as well as the content! quest bars http://j.mp/3C2tkMR quest bars

Wow, marvelous weblog structure! How lengthy have you ever been blogging for?
you made blogging look easy. The whole glance of your website is
fantastic, as well as the content!
quest bars http://j.mp/3C2tkMR quest bars

# Howdy would you mind sharing which blog platform you're working with? I'm looking to start my own blog in the near future but I'm having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design

Howdy would you mind sharing which blog platform you're working with?

I'm looking to start my own blog in the near future but I'm having
a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs and I'm looking for something completely unique.

P.S Apologies for being off-topic but I had to ask!

scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

# Howdy would you mind sharing which blog platform you're working with? I'm looking to start my own blog in the near future but I'm having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design

Howdy would you mind sharing which blog platform you're working with?

I'm looking to start my own blog in the near future but I'm having
a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs and I'm looking for something completely unique.

P.S Apologies for being off-topic but I had to ask!

scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

# Howdy would you mind sharing which blog platform you're working with? I'm looking to start my own blog in the near future but I'm having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design

Howdy would you mind sharing which blog platform you're working with?

I'm looking to start my own blog in the near future but I'm having
a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs and I'm looking for something completely unique.

P.S Apologies for being off-topic but I had to ask!

scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

# Howdy would you mind sharing which blog platform you're working with? I'm looking to start my own blog in the near future but I'm having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal. The reason I ask is because your design

Howdy would you mind sharing which blog platform you're working with?

I'm looking to start my own blog in the near future but I'm having
a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
The reason I ask is because your design and style seems different then most blogs and I'm looking for something completely unique.

P.S Apologies for being off-topic but I had to ask!

scoliosis surgery https://coub.com/stories/962966-scoliosis-surgery scoliosis surgery

コメントの投稿

タイトル  
名前  
URL
コメント