I have a socket class (mysocket) subclassed from SSLSocket with a property socktimer, in which I want to put a reference to my socketTimer class, which itself has a socket property in which I want to put a weak reference back to the socket. That way I avoid circular refs and all the relevant events (timerAction, socketDataAvailable, etc) will have access to the data they need to operate.
The expectation is that the socket gets used, then closed, and goes out of scope as the thread using it returns. Then I would expect the socket to get removed followed by the timer, as their refcounts go to zero.
The timer is instanced in the socket’s Constructor, with this as my first attempt:
Dim tim as socketTimer = new socketTimer
me.socktimer = tim // In the socket, a reference to the timer
tim.socket = SSLSocket (weakref (me)) // In the timer, a reference to the socket
Is this the right syntax for creating the weakref back to the socket? It compiles OK but I fear that all I’ve done is cast “me” twice.
Judging by some of the examples in the doc and also threads here, it looks more like I should change the socket property in the socketTimer class to be of type weakref, and then proceed thus:
Dim tim as socketTimer = new socketTimer
me.socktimer = tim // In the socket, a reference to the timer
tim.socket = new weakref (me) // In the timer, a reference to the socket
This also compiles and if correct then I assume that in the event handlers I would need to take socket.value and cast that to the appropriate socket class.
Is this a correct understanding?