« .NET GC & Interop クイズ | Main | 東芝 Dynabook SS M205 Tablet PC »

2004.03.14

.NET GC & Interop クイズ解答編

.NET GC & Interop クイズ の解答。
問題は Work メソッド内にある。Work メソッドが NativeMethod.WorkOnHandle を呼び出す時に

  1. this._handle をスタックに積む
  2. NativeMethod.WorkOnHandle
という手順を踏むが、この2つの手順の間で GC が発生した場合、this がすでにどこからも参照されていない可能性がある。.NET の GC では、メソッド呼び出しは呼び出されたオブジェクトを参照としてカウントしないためだ。
GC がこのオブジェクトを廃棄しようとしてファイナライザの ~Wrapper を呼び出すと、その中で NativeMethod.CloseHandle が呼び出されて this._handle は close されてしまう。その後に NativeMethod.WorkOnHandle が呼び出されるので、そこでエラーになってしまう。
正しい Work メソッドは以下のとおり。
  public void Work() {
    NativeMethod.WorkOnHandle(this._handle);
    GC.KeepAlive(this);
  }
GC.KeepAlive は特殊な IL にコンパイルされ、指定したオブジェクトをその実行時点まで GC に廃棄されないようにしてくれる。これで NativeMethod.WorkOnHandle から帰ってくるまで this が廃棄されないようにできる。
メソッド呼び出しの間にそのオブジェクトを廃棄しないような GC であればこの問題は発生しないが、MS の記事によるとそれをしてみたらパフォーマンスにかなり影響があったためこのような設計になっているらしい。
Interop は割と簡単に使えて .NET のよい部分の一つではあるが、理解が浅いとバグの温床になることを頭に入れておこう。

|

« .NET GC & Interop クイズ | Main | 東芝 Dynabook SS M205 Tablet PC »

Comments

I couldn't resist commenting. Perfectly written!

Posted by: Infromtion site | 2013.03.05 at 12:29 AM

Pretty! This was an incredibly wonderful post. Thank you for providing this info.

Posted by: Kara | 2013.03.11 at 10:20 PM

Hello, of course this piece of writing is genuinely fastidious and I have learned lot of things from it regarding blogging. thanks.

Posted by: dyno tuning South Africa | 2013.03.20 at 02:28 AM

Have you ever thought about creating an e-book or guest authoring on other websites? I have a blog based upon on the same information you discuss and would really like to have you share some stories/information. I know my viewers would enjoy your work. If you're even remotely interested, feel free to send me an email.

Posted by: PUA Forum | 2014.02.02 at 01:27 PM

It will make connecting with girls a whole lot more stressful once you have massive doubts in your thoughts. There are a few studies which might be gone above in depth, these since the 8 differing kinds of ladies, in addition to the one-minute brain examining process. Various other dating organizations, potentially much less good, manage services involving bootcamps for an end of the week where you can obtain personal instruction for around $1000.

Posted by: Pandoras Box Review | 2014.02.03 at 04:32 AM

Vin Di - Carlo's tactic consists of quite a few videos along with recommendations and there is no doubt it is quite comprehensive along with well designed technique. There are a few studies which might be gone above in depth, these since the 8 differing kinds of ladies, in addition to the one-minute brain examining process. Not really for starters plus a little bit expensive.

Posted by: christian hudson | 2014.04.01 at 06:58 AM

Greate article. Keep writing such kind of information on your blog. Im really impressed by it. Hello there, You've performed an incredible job. I'll definitely digg it and in my opinion suggest to my friends. I'm confident they'll be benefited from this website.

Posted by: rob judge pua | 2014.05.07 at 01:30 AM

Tɦe "Secret" Most Fat People Will Never Ҡnow About Losing Weіgɦt. The first reason is that it will ѕhow you steps on the best ways to stay away from stress ɑnd not binge on food. If you have done some research by yourself about Weight ʟоss Diet, and Weight Loss Ɍecipes in general, I am sure at some point of youг active interеst to find the right progrɑm, you started to realize a trend about "Effective and Legitimate Diet Programs" are bаsed of three сomponents: .

Posted by: reverse your diabetes today free | 2014.06.02 at 02:09 PM

I really like reading through a post that will make men and women think. Also, thanks for allowing for me to comment!

Posted by: Racing Moto Hack Tool | 2014.06.08 at 03:54 AM

Can I simply say what a comfort to find an individual who actually knows what they're discussing over the internet. You definitely understand how to bring an issue to light and make it important. More people should look at this and understand this side of the story. I was surprised you aren't more popular given that you definitely possess the gift.

Posted by: clash of clans android hack no download | 2014.06.08 at 07:34 AM

Heya i am for the primary time here. I came across this board and I to find It truly useful & it helped me out a lot. I hope to give one thing again and help others such as you helped me.

Posted by: pokerstars bonus code 2014 | 2014.06.08 at 07:40 AM

It's very straightforward to find out any topic on net as compared to textbooks, as I found this post at this web page.

Posted by: legit online jobs | 2014.06.08 at 08:01 AM

My partner and I stumbled over here different web page and thought I should check things out. I like what I see so now i am following you. Look forward to looking over your web page yet again.

Posted by: poker cheats | 2014.06.08 at 01:09 PM

Hello there! Do you know if they make any plugins to protect against hackers? I'm kinda paranoid about losing everything I've worked hard on. Any suggestions?

Posted by: best nba betting sites | 2014.06.09 at 09:58 AM

I've learn some just right stuff here. Definitely worth bookmarking for revisiting. I wonder how much attempt you place to create the sort of fantastic informative web site.

Posted by: new pop music producers | 2014.09.19 at 07:11 PM

ӏf some oone desirеs to be updated with most սp-to-date technologies afterward he must be visit this web page and be uρ to dɑte daily.

Posted by: cash and carry | 2014.09.22 at 02:19 PM

Quality posts is the important to attract the people to pay a quick visit the web page, that's what this website is providing.

Posted by: Hayden | 2015.02.16 at 11:56 AM

Post a comment



(Not displayed with comment.)




TrackBack

TrackBack URL for this entry:
http://app.cocolog-nifty.com/t/trackback/2111/297518

Listed below are links to weblogs that reference .NET GC & Interop クイズ解答編:

» [.NET] thisが参照されない? [is BUG ready ?]
[http://ii.cocolog-nifty.com/sasurai/2004/03/net_gc_interop_.html:title=.NET GC ... [Read More]

Tracked on 2005.03.25 at 05:49 PM

» Re: .NET GC & Interop クイズ [青柳臣一 blog : .NET や C# がメインの blog]
Re: .NET GC & Interop クイズ [Read More]

Tracked on 2005.03.25 at 09:48 PM

« .NET GC & Interop クイズ | Main | 東芝 Dynabook SS M205 Tablet PC »