problems with stackoverflowexception

Z

zapov

Hi!

I'm having some wierd problems with this exception (error).
If I use sql commands to insert data into sql server i get
strange behaviour from my application.

First I used a single threaded application and used
Application.DoEvents() when i needed to wait some time.
(I need to process some external information on events
so I can't just use Thread.Sleep).
I need to store some information on regular basis interval
(about 1 minute) so firstly i used xml to store information.
But after application executed for some time DataSet used to
store information would get destroyed (?!) and I would receive
Object not set to reference error. So I switched to SQL server.
First choice MSDE. I used stored procedures and executed them
from application, but application showed strange behaviour and
would sometimes stop when executing stored procedures for a few
seconds. Often it would stop and would not continue.
So I switched to MySQL and text queries. But I started to receive
StackOverflowException. I got confused and changed application
so it's not anymore a single threaded application and instead
of Application.DoEvents() I use Thread.Sleep
But strangely the Stackoverflowexception remained.

In application if instead of executing nonqueries I just return
from that function program works fine. There is nothing wrong
with sql part of the code, but I can't get a lock on the problem.
I'm getting desperate with this problem. Using .NET 1.1 with SP1
on WinXP SP2.
 
R

Robert Jordan

Hi,
I'm having some wierd problems with this exception (error).
If I use sql commands to insert data into sql server i get
strange behaviour from my application.

First I used a single threaded application and used
Application.DoEvents() when i needed to wait some time.
(I need to process some external information on events
so I can't just use Thread.Sleep).
I need to store some information on regular basis interval
(about 1 minute) so firstly i used xml to store information.
But after application executed for some time DataSet used to
store information would get destroyed (?!) and I would receive
Object not set to reference error. So I switched to SQL server.
First choice MSDE. I used stored procedures and executed them
from application, but application showed strange behaviour and
would sometimes stop when executing stored procedures for a few
seconds. Often it would stop and would not continue.
So I switched to MySQL and text queries. But I started to receive
StackOverflowException. I got confused and changed application
so it's not anymore a single threaded application and instead
of Application.DoEvents() I use Thread.Sleep
But strangely the Stackoverflowexception remained.

In application if instead of executing nonqueries I just return
from that function program works fine. There is nothing wrong
with sql part of the code, but I can't get a lock on the problem.
I'm getting desperate with this problem. Using .NET 1.1 with SP1
on WinXP SP2.


I guess the code that calls Application.DoEvents() is reentered,
like this:

void Idle() {
Application.DoEvents();
}

void SomeUIEventHandler(...) {
Idle();
}

Solution: use threads or assure that Application.DoEvents()
is never reentered.

bye
Rob

ps: don't switch technologies like your pants ;-) Choose
one and stick with it until it works.
 
Z

zapov

Robert said:
Hi,


I guess the code that calls Application.DoEvents() is reentered,
like this:

void Idle() {
Application.DoEvents();
}

void SomeUIEventHandler(...) {
Idle();
}

Solution: use threads or assure that Application.DoEvents()
is never reentered.

bye
Rob

ps: don't switch technologies like your pants ;-) Choose
one and stick with it until it works.

You didn't read my post carefuly. I said I changed application
so it uses threads and doesn't call app.doevents

ps. :) Of course. Goal was for MySQL, but I have more experience
in MSDE, so I tried that first
 
J

Joep

Whatever it is, something gets executed repeatedly unintended and consumes
stack on each iteration. It will not be SQL but code. You mention 'about 1
minute'? That sounds like an iterating thing, doesn't it? Without the actual
code can't say much more.
 
Z

zapov

Joep said:
Whatever it is, something gets executed repeatedly unintended and consumes
stack on each iteration. It will not be SQL but code. You mention 'about 1
minute'? That sounds like an iterating thing, doesn't it? Without the actual
code can't say much more.

Ok, but the problem is that if I don't use sql app works fine.
And if I use sql app crashes on first time it uses nonqueries
 
Z

zapov

zapov said:
Ok, but the problem is that if I don't use sql app works fine.
And if I use sql app crashes on first time it uses nonqueries

Hmmm, how could I check stack to see what's on it?
 
J

Joep

When you execute the SQL you have some code to process the result, don't
you? That is what I would look at carefully to start with. Without executing
SQL no processing of the result happens so my first guess would be that
processing. It most likely creates instances (direct or indirect) and is
reentered maybe, some event that gets triggered as part of processing is
also part of processing. The latter can be checked by adding some
instrumentation to show the number of iterations thru relevant function(s).
 
J

Joep

By the way, you also mentioned threading which can also be part of the
problem if it related to the SQL execution. Without code it's difficult to
be more precise, it remains theory at best.
 
Z

zapov

Joep said:
...and...is it reproducable when you run only a single thread?

Yes, the same problem appears with a single or multy threaded
application. The code is simple, it looks like this


public bool WriteUsage(string gdje,int koji)
{
Monitor.Enter(que1);
que1.Enqueue(obj1);
int k=0;
try
{
bool da=koji==1;
sqlcom.CommandText="INSERT INTO Zauzetost
VALUES("+gdje+",'"+DateTime.Now.ToString()+"','"+da.ToString()+"')";
if(sqlconn.State!=ConnectionState.Open) sqlconn.Open();
k=sqlcom.ExecuteNonQuery();
sqlconn.Close();
}
catch(Exception ex)
{
Monitor.Exit(que1);
LogError(ex);
return false;
}
Monitor.Exit(que1);
return k>0;
}

I ruled out the sql error because, this code works fine in other
applications. But, if I dont insert return true into first line of
this procedure, application crashes. :(
And no, as you can see, I don't use this part of the code to
process information, in fact, parts of the code with select
works fine and don't cause errors.
 
J

Joep

I see nothing wrong here. Without execution all is well, with execution you
end up with a stack o'flow, right? So does that apply to all INSERT
statements? What happens if you hard code the columns and the values ? Could
it be some mismatch between expected value and received value?
 
Z

zapov

Joep said:
I see nothing wrong here. Without execution all is well, with execution you
end up with a stack o'flow, right? So does that apply to all INSERT
statements? What happens if you hard code the columns and the values ? Could
it be some mismatch between expected value and received value?

Strangely, but no :)
The problem is that sometimes the code inserts values into database,
but when it doesn't exception raises.

I tried also using another database and protocol, so I gues it's not
drivers fault, or is it? :)
I guess, I'll have to try it without WinXP SP2
Because when using MSDE program ends up waiting in that code instead
of raising stackoverflowexception.
 
Z

zapov

Jon said:
<snip>

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.

Hm, no.
Event is raised by serial communcation with microcontroler so
it can be runned only on specific machine.

But you gave me a good idea, and if program ends up doing the same thing
I think I could try to post code tomorrow
 
W

Willy Denoyette [MVP]

The problem here is that you show us some code without any evidence that:
- the StackOverFlow exception is thrown by this part (that is, you don't
show us a stack trace)
- the "stack overflow" itself is due to this part of code, note that the
stack is shared by all code running on a thread, so it's perfectly possible
for one (badly behaving) method to consumes so much stack space, that
another (correctly behaving) method, fails because there's not anough free
stack space left.

Willy.
 
Z

zapov

Willy said:
The problem here is that you show us some code without any evidence that:
- the StackOverFlow exception is thrown by this part (that is, you don't
show us a stack trace)
- the "stack overflow" itself is due to this part of code, note that the
stack is shared by all code running on a thread, so it's perfectly possible
for one (badly behaving) method to consumes so much stack space, that
another (correctly behaving) method, fails because there's not anough free
stack space left.

Willy.

I don't have evidence that it's caused on this part of the code.
All I know is that if this code is not executed exception is not
raised.
And, yes, I'd like also to know whats on stack at failure time,
but when exception is raised, I don't get window with stack trace
but window with debug option (yes or no).
 
R

Richard Blewett [DevelopMentor]

Then why not put an exception handler around the call to this code to catch the exception and log it in a file or something similar so you can get hold of the exception trace, or put one in your Main just make sure you trap the exception somewhere so you can get a trace of what is exploding - otherwise we are shooting in the dark.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog


I don't have evidence that it's caused on this part of the code.
All I know is that if this code is not executed exception is not
raised.
And, yes, I'd like also to know whats on stack at failure time,
but when exception is raised, I don't get window with stack trace
but window with debug option (yes or no).
 
Z

zapov

Richard said:
Then why not put an exception handler around the call to this code to catch the exception and log it in a file or something similar so you can get hold of the exception trace, or put one in your Main just make sure you trap the exception somewhere so you can get a trace of what is exploding - otherwise we are shooting in the dark.

Regards

Richard Blewett - DevelopMentor
http://staff.develop.com/richardb/weblog

I even added appdomain.unhandledexception exception handler
but I can't catch the exception
I'll try to add it around the main, but I don't think that will help

I am shooting in the dark for a week now :(
 
R

Ravichandran J.V.

Z

zapov

Ravichandran said:
Please do post your code or exception trace. BTW, are you looking for
asynchronous operations?

with regards,

Thank you all guys, I found the problem
There was indeed a recursion in code :(
It would occur when I would try to log error.
If error could not be logged it would try to make another
error log :(

But still, I'm disapointed I could not see stack trace of
that problem.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top