Page 1 of 1

Stack corrupted with wxSQLITE3Database

Posted: Thu Mar 26, 2015 11:11 am
by aurusfeles
Hello,
I'm using Visual Studio 2010 en C++ for a project that uses wxWidgets and SQLite database. I'm using wxSQLite3 too, and with the simple example below, I have the error
Run-Time Check Failure #2 - Stack around the variable 'DB' was corrupted.
when quitting the method.

void DATABASE_INTERFACE::DBTest()
{

wxSQLite3Database DB;


DB.Open("c:/test/test.db");

DB.Close();
}

The only original thing in this method is that DATABASE_INTERFACE is a static class. Why am I having this error?

Re: Stack corrupted with wxSQLITE3Database

Posted: Thu Mar 26, 2015 7:17 pm
by utelle
aurusfeles wrote:I'm using Visual Studio 2010 en C++ for a project that uses wxWidgets and SQLite database. I'm using wxSQLite3 too, and with the simple example below, I have the error
Run-Time Check Failure #2 - Stack around the variable 'DB' was corrupted.
when quitting the method.
This error is most probably not related to wxSQLite3 at all. The place and time where stack corruption occurs does not tell you when the stack corruption actually happened. Stack corruption can occur if your program allocates more memory than is available on the stack or if your program overwrites parts of the code by using array indexes out of bounds.
aurusfeles wrote: void DATABASE_INTERFACE::DBTest()
{

wxSQLite3Database DB;


DB.Open("c:/test/test.db");

DB.Close();
}

The only original thing in this method is that DATABASE_INTERFACE is a static class. Why am I having this error?
From this code snippet it is impossible to tell what the cause of the problem might be. You have to show all the code that gets executed before calling method DBTest.

Regards,

Ulrich

Re: Stack corrupted with wxSQLITE3Database

Posted: Tue Mar 31, 2015 11:26 am
by aurusfeles
Thank you for your reply.
Actually, the code is really huge, I can't post it here. Is there any way to detect where the problem is, using Visual Studio or anything else?
Til then, i'm using Sqlite directly (I don't use any specific feature of wxsqlite, so it's a bit of overkill), and the stack corruption is "gone".

Re: Stack corrupted with wxSQLITE3Database

Posted: Tue Mar 31, 2015 2:40 pm
by doublemax
The only original thing in this method is that DATABASE_INTERFACE is a static class.
Does that mean the instance is a global variable? In that case it would be constructed before wxWidgets itself is initialized, which can have all sorts of negative side effects.

Re: Stack corrupted with wxSQLITE3Database

Posted: Tue Mar 31, 2015 7:50 pm
by utelle
aurusfeles wrote:Actually, the code is really huge, I can't post it here. Is there any way to detect where the problem is, using Visual Studio or anything else?
Well, if you can't boil down the code producing the problem to a reasonable size, there is little chance to help you to identify the cause of the problem. As said before stack corruption can be the result of different effects. Maybe your code writes to some arrays using indexes out of bounds, maybe your code allocates very large objects on the stack and thus exceeds the available stack size ... how should I know?
aurusfeles wrote:Til then, i'm using Sqlite directly (I don't use any specific feature of wxsqlite, so it's a bit of overkill), and the stack corruption is "gone".
Using SQLite directly reduces the amount of stack space needed, since SQLite allocates memory on the heap. Using a local variable within a method always allocates memory on the stack. So, if the cause of the problem is really a too small stack size, you could either increase the stack size in Visual Studio:

Code: Select all

Properties -> Configuration Properties -> Linker -> System -> Stack Reserve Size
or make the wxSQLite3Database object a class member instead of a local variable or use a dynamic object

Code: Select all

wxSQLite3Database* db = new wxSQLite3Database();
If on the other hand your code really corrupts the stack, i.e. by writing array elements out of bound, the above measures would not cure the original cause of the problem, and you could run out of luck at a later time at a different place in your application.

Regards,

Ulrich

Re: Stack corrupted with wxSQLITE3Database

Posted: Wed Apr 01, 2015 3:04 pm
by aurusfeles
I tried to increase the stack reserve to 8000000 with no effect.
or make the wxSQLite3Database object a class member instead of a local variable or use a dynamic object


Code : Tout sélectionner
wxSQLite3Database* db = new wxSQLite3Database();
I did this test too, and now I have a heap corruption detected when I delete de wxSQLite3Database object. Is this good news?

Re: Stack corrupted with wxSQLITE3Database

Posted: Wed Apr 01, 2015 4:31 pm
by utelle
aurusfeles wrote:I tried to increase the stack reserve to 8000000 with no effect.
or make the wxSQLite3Database object a class member instead of a local variable or use a dynamic object


Code : Tout sélectionner
wxSQLite3Database* db = new wxSQLite3Database();
I did this test too, and now I have a heap corruption detected when I delete de wxSQLite3Database object. Is this good news?
Certainly not.

However, it is extremely difficult to analyze such a problem without having access to the code and without knowing how you build the various components of your application.

For example, if your application consists of several DLLs and you used the static C++ runtime (compiler switch /MT) this could be a potential explanation for your problems, since then in MS VC++ each DLL has its own memory management.

wxSQLite3 is a very mature, actively maintained component. For sure, it's not the source of your problem. Most probably your application environment has a misconfiguration somewhere or an unfortunate side effect of the dynamics of your application are causing trouble.

Maybe activating option /RTC (https://msdn.microsoft.com/en-us/librar ... 00%29.aspx) might help you to track down your problem.

Good luck.

Regards,

Ulrich

Re: Stack corrupted with wxSQLITE3Database

Posted: Thu Apr 02, 2015 6:53 am
by aurusfeles
Thank you very much for your help. I'll try that. Each of your answers were very informative. I'll seek that sly error and find it.
Best regards