Hey, out of curiosity: If a class implements an interface, do you have to declare the object variable of the class as the interface type in order for the reference counting to free the object? Could I declare the object variable as the class type instead and have reference count freeing work? I say this because if I declare the object variable as the interface type, I CAN'T free it (compiler error), but if I declare it as the Class type, I can..

Comments

  1. Daniela Osterhagen "have mixed interface and object references. It works fine, as long as you understand what you are doing."
    I totally disagree! Everything works fine until somebody uses the object as an interface. And then you will get into deep trouble. 

    Let's take an example: You work in a team of five developers. There's a TConfig class implementing IConfig. There is one major instance of that class holding the configuration of the software. That instance is always referenced by its class type. Now there is someone changing a "T" to an "I" in the rarely used configuration dialog so that the config object suddenly becomes reference counted. Everything seems to work fine but after a while one of your users reports "an access violation when closing the application after it has been run for a long while". You have no idea where the error comes from and you try to reproduce the AV. After a couple of hours you want to give up saying "not reproducable" but others discovered the same bug in the meanwhile  "after the application has run all day". The bug report is incomplete. Of course. It has nothing to do with the up time of the application but the user has no idea how to reproduce the AV. The bug only occurs when you close the application after opening the config dialog and perform a specific action. You have already spent a lot of hours, maybe even days, in reproducing the bug. You see the problem comes from the mixed interface and class references for the same object. In the worst case you conclude your coworker made a mistake by referencing the config object via its interface type so you change it back to the class type and "fix" the bug. But it's only a matter of time when the bug strikes back again.

    The worst thing about that access violation is that the call stack of the exception doesn't lead you to the place where the bug has been introduced. Changing code in one unit can have bad effects in completely different moduls depending on what the user has done before. This is a classical Heisenbug: Hard to reproduce, hard to fix and really expensive. Mixing of interface and class references is producing Heisenbugs all over the place.

    ReplyDelete
  2. You can prevent declaration of variable as class type if you can declare your class in implementation section of unit and provide factory for creating its instances that returns interface.

    ReplyDelete
  3. Dalija Prasnikar Hi Dalija, I thought about that method, and we may do that in the future.

    ReplyDelete

Post a Comment