잊지 않겠습니다.

'.NET Gotchas'에 해당되는 글 2건

  1. 2009.01.07 Chapter 5. Garbage Collection Gotchas
  2. 2009.01.07 Chapter 4. Language Interoperability Gotchas

In C++ or No GC enviroment, we have two serious problems in windows programming.

  • deletes other objects to which the object holds pointers
  • closes handles to resource

And .NET cannot release unmanaged resource automatically. so .NET prepared Finalize() method to dipose unmanaged resource.

In VB.NET : actually write the Finalize() method

In C# : pseudo-destructor with the ~NameOfYourClass() -> C# Compiler creates the Finalized() method using the code when we write in the specialized pesudo-destructor.

 

Writing Finalize() is rarely a good idea

if you don't use unmanaged resource, Don't write the Finalize() method. It make inaccessible instances and delay the cleanup.

 

Releasing managed resources in Finalize() can wreak havoc

Don't access managed resources with in the Finalize() method. If you access managed resource, it cannot be predicted.

Also, don't call any methods on other objects, or even set their reference to null/Nothing.

 

Rules to invoke base.Finalize() aren't consistent

In VB.NET, you write Finalize() method as a protect method but you only write pseudo-destructor in C#.

so you don't invoke the base class's Finalize() method in C# but you must invoke the base class's Finalize() method in VB.NET.

 

Depending on the Finalize() can tie up critical resources

.NET provides a mechanism to support synchronous cleanup fo resource used by object. : IDisposable interface

If you want to clean up your unmanaged resource synchronously, you can write Dispose() method used IDisposable interface.

 

Using Finalize() on disposed objects is costly

It's better to Dispose() an object than to depend on Finalize() method because Dispose() let you control the clean up of an object.

and Dispose() should have already taken care of its resources, both managed and unmanaged resources.

Calling Finalize() on an object involves overhead. This should be avoided as mush as possible. From within your Dispose() method, call GC.SuppressFinalize().

신고
Posted by xyzlast Y2K

What are the things you need to remember to make your code interoperable between language?

Common language specification complicance isn't the default

Operation overload : not Common Language Specification(CLS)

CLS-Compliant : add [Assembly:system.CLSCompliant(true)] 를 넣어줘야지 된다.

이는 Language가 다를때, 서로간에 CLS에 대해서 명확한 정의를 따르는지에 대한 Compiler level에서의 정의가 될 수 있다.

 

Optional parameters break interoperability

VB.NET에서 허용되는 option parameter의 경우에는 C#에서는 허용되지 않기 때문에, 반드시 그 값을 적어줘야지 된다.

 

Mixing case between class members breaks interoperability

VB.NET에서는 각 값의 case에 따라서 다른 Property, member, method를 가질 수 있다. 이는 VB.NET과 C#을 섞어서 사용하는 경우에 CLS를 위반하게 된다.

 

Name collision with keywords breaks interoperability

VB.NET에서 사용되는 Stop()의 경우에는 code break로 사용되기 때문에, 이름에 의한 충돌이 일어날 수 있다. 언어의 종류에 따라 method의 이름을 명명할 때 주의해야지 된다. 

 

Defining an array isn't interoperability

VB에서 자주 일어나던 문제. VB의 경우에는 Array의 크기의 선언이 아닌 Array의 max index의 선언이기 때문에 발생

int[] intDatas = new int[3]; <- size : 3
Dim intDatas() As Integer = new Integer(3) {} <- size : 4


신고
Posted by xyzlast Y2K


티스토리 툴바