views:

312

answers:

7

I have recently taken over a legacy windows service and it has been writing the following event in the system event log:

Event ID: 7034
Description: The MyService service terminated unexpectedly. It has done this X time(s).

I was looking over source code and found the following code pattern in the service class library: (It has been simplified to protect the innocent..)

public static void StartService()
{
    //do some stuff...
    ManageCycle();
}

public static void ManageCycle()
{
   //do some stuff
   ManageCycle();
}

What is this coding patten called and could it possibly cause the windows service to shutdown (i.e. memory leak)?

+1  A: 

That's a recursive call that will ultimately blow the stack.

Forgotten Semicolon
...unless it has a conditional exit that we are not seeing.
Robert Harvey
There is not a conditional exit in the code.
Michael Kniskern
@Michael: It doesn't matter, this is not a place for a recursive calling technique.
Eran Betzalel
+2  A: 

It suppose to throw StackOverflow (HA HA :) ) Exception, because of the endless recursive calling.

Take a look at this example - you should choose the technique that fits your architecture.

Eran Betzalel
A: 

This is a recursive call with apparently no exit criteria. Eventually it will run out of stack since a call to ManageCycle never returns.

In addition the StartService method will never return, it ought to be spining up at least one foreground thread and then return.

AnthonyWJones
A: 

Recursion, it ooks like it's recursively calling itself. I'm surprised there isn't a stack overflow exception. Perhaps the service property on the machine running this is configured to restart the service on failure.

kragan
+2  A: 

This looks like the stack overflow exception pattern. Eran is correct. Use a while loop:

public static void StartService()
{
    //do some stuff...
    isRunning = true;
    ManageCycle();
}

public static void ManageCycle()
{
   while(isRunning)
   {
   //do some stuff and wrap in exception handling
   }
}

public static void StopService()
{
    isRunning=false;
}
Matt Wrock
A: 

It's recursive alright. It will keep calling itself repeatedly (a bad thing) and that will result in a stackoverflow.

What the "//do some stuff" do? Maybe there is a good reason that it calls itself, bBut without a way to get out of the loop (recursive), the application will exit.

Khan
the //do some stuff piece of the service call a database to retrieve a collection of monitors to check the health of web pages, windows services, hard drive capacity and whethor or not a device is pingable.
Michael Kniskern
+1  A: 

The best answer for this kind of situation: Don't use Recursive Algorithms unless your algorithm has a recursive structure. For example, if you're analyzing a file system, and want to scan a specific Directory, you'd want to do something like:

void ScanDirectory(Directory)
{
    // Handle Files
    if (currfile.directory)
        ScanDirectory(currfile)
}

This makes sense because it's much easier than doing it iteratively. But otherwise, when you're just repeating an action over and over again, making it a recursion is completely unnecessary and will cause code inefficiency and eventually stack overflows.

Ori Osherov
+1 This is the right conclusion for this question. It's more of a "how to code right?" question than "how to achieve code perfection?" question.
Eran Betzalel