Essentially, you are doing it right. Your drives are reasonable, and you understand the need to do manual reference counting, because you keep the link in a field like Pointer that doesn't do reference counting.
P := ISuperObject(Node.Data);
If P to nil , this means that Node.Data is nil . Nothing more to say. Presumably, there is some pretty mundane reason for Data be nil , but that has nothing to do with how you cast.
Looking at my code, I would criticize it for confusing all the different problems. You can easily find this task if you can maintain a certain degree of isolation between different aspects.
One way to make life much easier is to avoid using the untyped Data pointer. Instead, use a custom node type that can do the correct reference counting:
type TMyTreeNode = class(TTreeNode) private FIntf: IInterface; property Intf: IInterface read FIntf write FIntf; end;
You need to handle the OnCreateNodeClass event of the tree view to get a control to create the node class.
procedure TForm1.TreeView1CreateNodeClass(Sender: TCustomTreeView; var NodeClass: TTreeNodeClass); begin NodeClass := TMyTreeNode; end;
Now, whenever a tree view control creates an instance of node, it creates one of the TMyTreeNode types. Which has a field to contain your interface. I typed it as IInterface here, but you would use a more specific interface that would suit your needs. And, of course, you can add any possible function to your own node type.
The soft binding to this is that you need to pass the node links from TTreeNode (as returned by the underlying tree control) to TMyTreeNode in order to access the interface property. However, this binding is worth it, in my opinion, because you can correctly rely on the compiler for a controlled lifetime, and therefore forget about this aspect of the code. This allows you to focus on your program, rather than on a tedious template. Continuing the path that you are on now looks like a recipe for memory leaks and access violations. Get a compiler for managing things, and you can be sure you won't make such mistakes.