Originally shared by Jeroen Wiert Pluimers
Originally shared by Jeroen Wiert Pluimers
A very interesting discussion on what the mixing of interfaces and classes can get you into: [WayBack] I’ve got an interface with generics which accepts classes:IMyGeneric = interface…..end;What I want to do is to allow this interface to… – John Kouraklis…
http://wiert.me/2018/03/15/delphi-do-not-mix-interfaces-and-classes-part-x/
A very interesting discussion on what the mixing of interfaces and classes can get you into: [WayBack] I’ve got an interface with generics which accepts classes:IMyGeneric = interface…..end;What I want to do is to allow this interface to… – John Kouraklis…
http://wiert.me/2018/03/15/delphi-do-not-mix-interfaces-and-classes-part-x/
do not mix interfaces and classes AND generics :)
ReplyDeletedon't mix interfaces and classes means - if your reference counted object instance starts its life the wrong way with wrong reference count then it will not live long and prosper but will die the moment it bumps into any code that triggers reference counting mechanism.
ReplyDeleteConsequences may vary from nothing happening, over getting weird results to crashing,
Reference counted object instances must have at least one strong reference to keep them alive. Interface reference represents such strong reference. Object reference acts like weak one.
CHUA Chee Wee That is exactly what happens in your code.
https://chuacw.ath.cx/blogs/chuacw/archive/2018/02/13/something-to-ponder-using-interfaces-with-class-type-variables.aspx
You are storing object in object reference and it has wrong reference count, first call to InAnother routine triggers reference counting mechanism because parameter is not const, and when it exits the routine object is destroyed. Since strings are managed types destroying the object will also destroy the string - so it will no longer have any value - and integer will still be there lingering in memory untouched unless something else writes over it...
Dalija Prasnikar Haha, no, it's not my code. It's somebody else's code I've fixed. ;op
ReplyDeleteCHUA Chee Wee ;P
ReplyDelete