Wednesday 2 March 2011

CMString - lifetime and ownership

So, you have a CMString and you've noticed that it is IDisposable. But you're passing it in and out of functions. What is the lifetime of your CMString? Which reference has ownership, especially as Sophis have implemented their own reference counting class?

Here we have it then:

use CMString.ToString, CMString.GetString or the implicit cast operator to System.String and testing would suggest that this is safe. These three functions appear to perform copies.

assign another CMString to your first CMString and then Dispose the first one. Miraculously this works too! Even though it can be clearly seen that both CMStrings are in fact referring to the selfsame underlying data! Hurrah!

Nevertheless, don't rely on the CMString because of the unmanaged aspect. Always convert to System.String!

No comments:

Post a Comment