Jonas Raoni wrote: > Since the TStream is shared, any of these items can't free the TStream > if there's someone using it... I was going to implement a reference > count on my class or maybe create a "TTree" which would hold > "TTreeNodes" and it would be able to free the TStream, but then I > remembered of Interfaces =]
Having a tree object to manage all its nodes would probably be a good idea anyway. I'm surprised you haven't needed one sooner. > I took a look on the help about interfaces, and I've made the > following code based on that TInterfacedObject... There is a standard Windows interface, IStream, and Delphi provides a class that adapts a TStream instance to that interface, TStreamAdapter, in the Classes unit. Rewrite your tree-node classes to use an IStream instead of a TStream, and then create a TStreamAdapter to wrap your TFileStream instance. > TInterfacedStream = class( TFileStream, IInterface ) > private > FRefCount: Integer; > function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall; > function _AddRef: Integer; stdcall; > function _Release: Integer; stdcall; > public > procedure AfterConstruction; override; > procedure BeforeDestruction; override; > class function NewInstance: TObject; override; > property RefCount: Integer read FRefCount; > end; > > But after testing I see the Interface isn't auto-freeing :) > > So... Am I doing it right haha? It's hard to tell without seeing your code. Merely declaring something as implementing an interface doesn't magically make it destroy itself. You need to tell it to destroy itself when it reference count reaches zero. You also have to refer to the object via an interface variable, not an object variable. Reference counting occurs as interface variables go into and out of scope; if there are no interface variables, then there are no references to count. -- Rob ----------------------------------------------------- Home page: http://groups.yahoo.com/group/delphi-en/ To unsubscribe: [EMAIL PROTECTED] Yahoo! Groups Links <*> To visit your group on the web, go to: http://groups.yahoo.com/group/delphi-en/ <*> To unsubscribe from this group, send an email to: [EMAIL PROTECTED] <*> Your use of Yahoo! Groups is subject to: http://docs.yahoo.com/info/terms/

