Using AddHandler In Service App. System Crashing.

I have a service application that has been running fine. I added code to use a private thread and a private timer into a sub class of tcpSocket and now my app is crashing. Not a debugger crash but a windows crash, so I really have no idea where the problem is in the code. I’m unsure if what I am doing is bad practice or not. I was hoping someone on here could tell me if what I am doing is causing a system issue leading to a windows crash or if I should report this as a bug with my crash dump.

I have a sub class of TCPSocket in a service app. I have a private thread property and private thread handler method. I have a private timer property and private timer handler method.

In the DataAvailable event I am initializing them both like this when all of my data has come into the socket:

[code] tmrParse = New Xojo.Core.Timer
tmrParse.Period = 700
tmrParse.Mode = Xojo.Core.Timer.Modes.Multiple
AddHandler tmrParse.Action, AddressOf TimerAction

ThdResponse = New Thread 
AddHandler ThdResponse.Run, AddressOf ThdResponse_Run
ThdResponse.Run[/code]

So each time a connection is made a new thread and timer are initialized. I’m not sure if this over time is causing the app to crash. It runs for a few minutes each time and then I get a windows crash.

So I decided to try to remove the handler after each message was sent back to the client to see if that helps but I can’t seem to remove the timer handler either using the exact code from the dev site it says, this item does not exist on the Sender.Mode line?

' Stop Timer and Remove the handler Sender.Mode = Xojo.Core.Timer.Mode.Off RemoveHandler tmrParse.Action, AddressOf TimerAction

I’m not sure of the sender line for the thread if there is an equivalent or not. I tried this:

' Remove the handler Sender.Mode = ThdResponse.Kill // This line doesn't compile, maybe I don't need this line to remove the thread handler RemoveHandler ThdResponse.Run, AddressOf ThdResponse_Run

Am I initializing these handlers wrong? I know if it’s a Desktop app you initialize them in Open event. Of course there is no Open event in the service app, I tried to initialize them in run using a public method in the TCPSocket sub class but I kept getting a NilObjectException.

Ugh rough day, any advice please? Thanks…

On first glance, it looks like you’re replacing tmrParse in every dataAvailable event. I think it will persist and contiue to fire as it’s in mode.multiple (maybe), but your removehandler call will affect the current timer in tmrParse, probably not what you want.
Is Sender defined somewhere?

The timer is looking for a boolean to be true from the completion of a thread that is parsing the data input from the socket. I am removing the timer handler like this after each message is returned to the client from the socket:

' Stop Timer and Remove the handler sender.Mode = Xojo.Core.Timer.Mode.Off //This line won't compile straight from the dev RemoveHandler tmrParse.Action, AddressOf TimerAction

Sender is defined in my AddHandler method for my timer called TimerAction also straight from the Dev.

Thanks.

Jim is correct. If you AddHandler, it’s up to you to RemoveHandler too or the object will continue to run. If you create one on every iteration, you are creating a memory leak.

Also it’s best to use WeakAddressOf rather than AddressOf or your host object will never go away.

Try something like this:

if tmrParse is nil then
  tmrParse = New Xojo.Core.Timer
  AddHandler tmrParse.Action, WeakAddressOf TimerAction
end if
tmrParse.Period = 700
tmrParse.Mode = Xojo.Core.Timer.Modes.Multiple

// Elsewhere ...

if tmrParse isa object then
  tmrParse.Mode = Xojo.Core.Timer.Modes.Off
  RemoveHandler tmrParse.Action, WeakAddressOf TimerAction
  tmrParse = nil
end if

It runs about ten minutes or so each time after processing about 100 or less messages from a client and then I get a windows crash. I was guessing it was caused by the handlers just adding up over time and causing a memory issue. I’ll run this code for a while and see if it helps. THANKS!!! :slight_smile:

How do I properly remove my threads I am currently doing this to create in DataAvailable and this to Remove in the Thread handler method:

ThdResponse = New Thread AddHandler ThdResponse.Run, WeakAddressOf ThdResponse_Run ThdResponse.Run

RemoveHandler ThdResponse.Run, AddressOf ThdResponse_Run

@Kem Tekinay I added the code you suggested and after a few minutes it crashed with this:

Not sure what to tell you as I use code like that all the time without issue. Your next option is to abandon AddHandler and do it the old-fashioned way with subclasses.

Ok.

Thanks for all your help and everyone else that has helped me today. You guys are great and I really appreciate all your time. :slight_smile:

@Kem Tekinay If I use sub classes for the thread and timer will a new instance of the thread and timer classes be created for each new connection of my TCPSocket sub class? I used AddHandler originally so I could make the timer and thread private to the TCPSocket sub class. I’m trying to keep each connection to my server as separate from the others as possible.

I would create the instance of the timer & thread along with handlers in the constructor and remove handlers in the destructor for the socket, then in the dataavailable event handler you can set the mode of the timer to start it and of course run the thread. After the thread has completed set the mode of the timer to modeoff.

What Wayne said. :slight_smile:

@Wayne Golding So where exactly in my program would you consider to be the constructor? That’s what I am trying to figure out. I am creating the thread and timer and the handlers in the data available event. Where should I be creating them?

@Wayne Golding You mean where I create the TCPSocket in the ServerSocket AddHandler event?

See http://documentation.xojo.com/index.php/Constructor

Just a note @Kem Tekinay I moved the Remove Handler code from the end of the timer method handler to the Send Complete event of the TCPSocket and it hasn’t crashed yet.

not having a remove handler is likely to eventually give you a different problem

@Wayne Golding @Kem Tekinay @Norman Palardy This is the first time I’ve used AddHander so I am trying to figure out where to initialize it and how to control removing / stopping the timer thread etc.

I create the TCPSocket in the Add Socket event of the ServerSocket. Is this the best place to create the instances of the timer and thread?

The client app controls the closing of the socket, it’s a java based client, I don’t have the TCPSocket close anywhere on the Xojo side.

Thanks for everything guys!!! :slight_smile:

are the timer & thread associated with JUST that socket ?
forever ?
if so that would make sense

and put the remove handlers in the destructor for this (which may mean you need a custom subclass which I suspect you have anyways)

just to drive kem nuts :slight_smile:

[quote=273687:@Kem Tekinay]Jim is correct. If you AddHandler, it’s up to you to RemoveHandler too or the object will continue to run. If you create one on every iteration, you are creating a memory leak.

Also it’s best to use WeakAddressOf rather than AddressOf or your host object will never go away[/quote]

Interesting eye openers … thanks.

@Norman Palardy Yes the Timer and Thread are both created along with the AddHandler methods just for use by the socket. They are currently being created in the DataAvailable Event of the TCPSocket. The thread parses the incoming message data and the timer controls when to send the response from the socket.

I don’t have a deconstructor do I? I don’t actually close the socket in Xojo, I close it on the Java side.

Thanks!!