The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 2,119 other followers

Don’t mix objects and interfaces

Posted by jpluimers on 2020/06/11

Worth repeating Dalija Prasnikar, because I bumped into sources of people not getting this concept: [WayBack]: Don’t mix objects and interfaces.

You should not, but sometimes it is hard not to.

The rules in his article are very important as they indicate what’s safe and what’s not:

  1. object references are weak, interface references are strong
  2. reference counted object instances need at least one strong reference to keep them alive therefore you can’t use object reference as primary owning reference
  3. reference counted object instances can be safely accessed through temporary, short-lived (weak) object references, as long as there are strong reference(s) keeping the object alive

So basically:

  • ensure a reference counted instance has at least one strong reference to manage the lifetime
  • temporary (short-lived) weak references are OK as long as you are sure the strong reference lives longer

Note that unsafe will make interfaces references weak instead of strong; the same holds for pointer lists [WayBack] Using the [unsafe] attribute, it is possible to store the interface without reference counting. Is there any way for this behavior to be “transferred” … – Jacek Laskowski – Google+

–jeroen

via: [WayBack] Dalija Prasnikar – Google+

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

 
%d bloggers like this: