Unmanaged code and NullReference Exception problem (PLEASE HELP)

S

SpookyET

I want to create a wrapper for SQLite and I'm having a problem with getting
NullReferenceException when calling the unmanaged function. I do not wish to
write in C++ since i do not have enough experience with it. The problem I'm
having is that after some 17 rows I get NullReferenceException while there
are more than 200 rows in the table. SQLite is an embeddable C sql server,
very fast and very small. www.sqlite.org
I cant use MSDE do to licence issues and size issues. I can't put a 54MB
database inside a 1mb app. SQLite is 250K.
I didn't pate this code, i typed it so there might be some types of var
names.

Main()
{
...
IntPtr sqliteHandler = SQLiteWrapper.Open(path, mode, out errorMessage)
CallbackFunctionHandler callBackFunctionHandler = new (CallbackFunction);
// I have even tried to put GC.KeepAlive(callBackFunctionHandler) here but
doesn't work
// it shouldn't go out of scope anyway
SQLiteWrapper.Execute(sqliteHandler, "SELECT * FROM Users",
callBackFunctionHandler, IntPtr.Zero, out errorMessage); // it does this 17
times and cracks
}

unsafe public static int CallbackFunction(IntPtr userData, int
columnsNumber, sbyte** columnsData, sbyte** columnsNames)
{
for (int i = 0; i < columnsNumber; i++)
{
string column = new String(columnsNames));
string result = new String(columsnData));
Console.WriteLine("{0} = {1}", column, result);
}
}


Original Function
int sqlite_exec(
sqlite*, /* An open database */
const char *sql, /* SQL to be executed */
sqlite_callback, /* Callback function */
void *, /* 1st argument to callback function */
char **errmsg /* Error msg written here */|
);

C# Wrapper
[DllImport("sqlite.dll", EntryPoint="sqlite_exec",CharSet=CharSet.Ansi,
ExactSpelling=true)]
public static extern int Execute(IntPtr sqliteHandler, string sqlCommand,
CallbackFunctionHandler callBack , IntPtr userData, out string
errorMessage);

C# CallBackFunction Pointer
unsafe public delegate int CallbackFunctionHandler(IntPtr userData, int
argc, sbyte** argv, sbyte** colnames);

Original Function Pointer implementation
typedef int (*sqlite_callback)(void*,int,char**, char**);

There is another way to get records from SQLite but it returns char*** which
is a pointer to an array of pointers to strings (C strings)

i tried sbyte*** foo
and new String(*foo[0]) but i get nothing, tried with char too instead of
sbyte

Please help.
I thank you in advance.
 
M

Mattias Sjögren

Main()
{
...
IntPtr sqliteHandler = SQLiteWrapper.Open(path, mode, out errorMessage)
CallbackFunctionHandler callBackFunctionHandler = new (CallbackFunction);
// I have even tried to put GC.KeepAlive(callBackFunctionHandler) here but
doesn't work
// it shouldn't go out of scope anyway
SQLiteWrapper.Execute(sqliteHandler, "SELECT * FROM Users",
callBackFunctionHandler, IntPtr.Zero, out errorMessage); // it does this 17
times and cracks
}


You should put the GC.KeepAlive call after the Execute call, not
before. See if that helpst keeping the delegate valid.

C# Wrapper
[DllImport("sqlite.dll", EntryPoint="sqlite_exec",CharSet=CharSet.Ansi,
ExactSpelling=true)]
public static extern int Execute(IntPtr sqliteHandler, string sqlCommand,
CallbackFunctionHandler callBack , IntPtr userData, out string
errorMessage);

Also, I think it would be better if you changed the last parameter to

out char* errorMessage

or

out IntPtr errorMessage



Mattias
 

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