跟小D每日学口语

DependencyProperty重复注册引起的bug

这两天碰到一个非常奇怪的bug,在某些PC上都是正常的, 独在一台机上直接crash掉,有handler app exception也catch不到,出的是clr的错误(http://msdn.microsoft.com/en-us/magazine/cc793966.aspx 根本不知道所云),又不方便debug(在美国,我这边网络不太好,想remote debug很慢)。想了很久不知道什么原因。

最后只能让我老板(在美国)去DEBGU才发现有两个usercontrol1和usercontrol2 register dependency property都是一样的包括typeof 指向的都是同一个usercontrol1). 但是usercontrol2从来没有用到这个property, 而且两个usercontrol也不会同时加载在UI。我测试了一下如果同时使用在就会报错,但是不同时使用就不会。

最后我问他原因他是这样解释的: 

the sequence of static constructor is undefined

I think it's related to the loading of the machines and the memory consumption

and then CLR may decide to run it earlier

CLR may decide it doesn't need to run it at all

note that the problem is at usercontrol2 

which I never need

so, it's not surprised that CLR doesn't call the static constructor of usercontrol2 in your machine

if it doesn't call that static method, you won't have problem.

我想也是这样的原因,因为clr它在不同的PC上的内存管理不一样,会去决定预加载一些将会用到的组件,可能在有些PC上它会同时预加载usercontrol1和useronctro2,所以就会报错。但不管怎么说,我都觉得这应该是microsft的一个bug.

而且这样的问题以后很难在开发的过程中可以检查出来的。 

posted @ 2010-10-29 11:41  javak  阅读(2680)  评论(1)    收藏  举报