DLL return values, MANY CRASH error! - SOLVED

Posted By: NeoNeper

DLL return values, MANY CRASH error! - SOLVED - 07/05/13 17:25

Hello friends!.
I created a dll made ​​in C + + to return values ​​that were surveyed in a database SQLITE3!

At first everything works correctly, BUT , when the database starts to earn enough database tables start getting bigger is that my real problem starts! The operation is totally unstable!

Suddenly I get an crash error.
These errors happen when I need to receive the data.
But not always. Sometimes it works fine and others it can generate a crash error, which I can not identify.

But I managed to identify these occasional errors always happen when I use search functions of data. When I need to get a large number data is then subject to severe receiving a crash error.

could you give me tips so I can STABILISE my function?

Sample my DLL and LiteC Script:

C++
Code:
//Receive data from rows and columns of the DataBase
...
DLLFUNC char* sqlite__select(int r, int c)
{
 //return char value on rows [r] and Column [c]
  return ((char*)result[r][c].c_str());
        
}
...



LiteC, Receive Many data.

Code:
void test_select()
{

int rows = sqlite_rows(); //Total de rows (sample 100 rows)

for(ping = 0; ping < rows; ping ++)
{
//ID			
printf("%s",sqlite_select(ping,0)); //return 0..1..2..3..4..n
//test0
printf("%s",sqlite_select(ping,1)); //return 0..1..2..3..4..n
//test1
printf("%s",sqlite_select(ping,3)); //return 0..1..2..3..4..n
.... test100

}



Can happen or not returning a crash error. (T.T)



Posted By: HeelX

Re: DLL return values, MANY CRASH error! - 07/05/13 17:55

I could imagine that returning the c_str() is not a good idea. Maybe you pass a char* buffer instead, in which you write the result?

Code:
DLLFUNC char* sqlite__select(char* buffer, int r, int c)
{
    if (buffer != NULL)
        strcpy(buffer, result[r][c].c_str());
        
    return buffer;
}



Code:
void test_select ()
{
    int rows = sqlite_rows();
    
    char buffer [256];
    
    int ping;
    for (ping = 0; ping < rows; ping++)
    {
        printf("%s", sqlite_select(buffer, ping, 0));
        printf("%s", sqlite_select(buffer, ping, 1));
        printf("%s", sqlite_select(buffer, ping, 3));
    }
}

Posted By: NeoNeper

Re: DLL return values, MANY CRASH error! - 07/05/13 18:19

ooh THAT AND A really good idea.
Testing will!
Posted By: NeoNeper

Re: DLL return values, MANY CRASH error! - 07/05/13 18:59

seem to work!
tested many again my game, and I had no more problems! (^.^) Tankx HeelX.

If you have a few more important tips for working with DLL, please teach for me.
(^.~)
Posted By: Wiseguy

Re: DLL return values, MANY CRASH error! - 07/05/13 20:31

Originally Posted By: NeoNeper
If you have a few more important tips for working with DLL, please teach for me.
(^.~)

I have told you about the problem with auto variables multiple times before in this thread, but you chose to ignore it. And I don't doubt that you will do the same mistake again in the near future.

Important tip: PLEASE get familiar with C++ before doing anything else. That your program crashed was very fortunate, because it's an easy way to track down undefined behaviour, but you are not guaranteed to be that lucky every time.

Also, in a similar vein, DON'T use old C-Style casts! They are too broad and you lose an awful lot of control when using them. And don't cast away constness if you don't need to, and if you absolutely need to, use const_cast<>().
Posted By: NeoNeper

Re: DLL return values, MANY CRASH error! - 07/08/13 17:57

tanks Wiseguy.
Some times I have difficulty understanding, for not mastering the English language fluently.

I have trouble with C + +, but I'm slowly learning, with your help and support materials. I am so grateful to this community.

I have done great things using LITEC along with C + +!
The LITEC became a door for me to enter this world of programming games and consequently for me to learn new programming languages!.

Posted By: HeelX

Re: DLL return values, MANY CRASH error! - 07/08/13 19:06

NeoNeper, there are just a few things to remind when working with C++ DLLs and Gamestudio. Most important is the fact that you have to use the _VAR() macro to turn numeric values into the var-representation, when passing to Gamestudio functions. The same goes for the other way round, please use always the _INT() and _FLOAT() macros to convert vars into numeric values.

Beside that, there are no special advices we can give you, since all other advices might be focused on C++ and for that, you can simply buy a book that handles that better than we can.
Posted By: NeoNeper

Re: DLL return values, MANY CRASH error! - 07/10/13 16:04

Tanks HeelX (^.^).
© 2024 lite-C Forums