I've been trying to investigate this problem (running
Mono.Data.PostgreSqlClient compiled with CSC under .NET on Windows) and
almost managed to solve it (well, kind of) ;-)

I've discovered the following problems (patch for cases 1,3 is attached, but
consider it highly experimental - don't commit):

1. PostgresLibrary.cs fails to marshal string return values correctly. MS
docs/samples say that when you declare a function that returns a pointer to
internal buffer, like getenv() under Unix or GetCommandLine() under Win32,
you cannot marshal is as "[DllImport()] static string whatever();", but
instead you must use the following construct (taken from my patched
PostgreSqlClient)

===========================================
[DllImport("pq",EntryPoint="PQresultErrorMessage")]
private static extern IntPtr PQresultErrorMessage0 (IntPtr res);

public static string PQresultErrorMessage(IntPtr res)
{
    return Marshal.PtrToStringAnsi(PQresultErrorMessage0(res));
}

===========================================

When you write "static string function(parameters)" the function is expected
to ALLOCATE the returned string on task heap (using CoTaskMemAlloc() under
Windows), so CLR will happily free the string when it's no longer used, but
unfortunately when you return the static pointer to the CLR, it will attempt
to free an unallocated memory block, which is a big BOOM.

Source:
FrameworkSDK/Samples/Technologies/Interop/PlatformInvoke/WinAPIs/CS/Buffers.
cs

2. This also means that mono's marshalling is somehow broken, or the memory
isn't properly released in this case. I didn't look deep into it, so I
cannot tell you more.

3. The third (very minor) problem with PostgreSql under windows are the
lines in PgSqlCommand.cs which cause exception (I don't remember what it
was, but swapping the rows helped eliminate the exception).

      schemaRow.AcceptChanges();
      dataTableSchema.Rows.Add (schemaRow);

4. The last, but the BIGGEST PROBLEM still remains unsolved. The whole thing
works in Debug mode but not in Release under Windows. In release it seems to
corrupt the memory, so that String.Format() returns an exception. Maybe the
corruption also occurs in Debug mode, but is somehow masked, I don't know.

The following (the simplest one I could come up with) snippet fails under
Release but works under Debug

IntPtr ip = Mono.Data.PostgreSqlClient.PostgresLibrary.PQconndefaults();
Console.WriteLine("test {0}", 1);

with

Unhandled Exception: System.IO.IOException: The handle is invalid.

   at System.IO.__Error.WinIOError(Int32 errorCode, String str)
   at System.IO.__ConsoleStream.Write(Byte[] buffer, Int32 offset, Int32
count)
   at System.IO.StreamWriter.Flush(Boolean flushStream, Boolean
flushEncoder)
   at System.IO.StreamWriter.Write(Char[] buffer, Int32 index, Int32 count)
   at System.IO.TextWriter.WriteLine(String value)
   at System.IO.TextWriter.WriteLine(String format, Object arg0)
   at System.IO.SyncTextWriter.WriteLine(String format, Object arg0)
   at DBTool.DBToolMain.Test()
   at DBTool.DBToolMain.Main(String[] args)

I've got no clue here. Anyone can help?

Jarek

----- Original Message -----
From: "Weiss-Graef Dieter" <[EMAIL PROTECTED]>
To: <[EMAIL PROTECTED]>
Sent: Wednesday, January 08, 2003 6:52 PM
Subject: Postgresql client on Windows


Hi,

have you found any solutions for this (I have the same problems):

http://lists.ximian.com/archives/public/mono-list/2002-November/003625.html
?

Dieter

-------------------------------------------
Fachhochschule Bonn-Rhein-Sieg
University of Applied Sciences
Dieter Wei�-Gr�f

Grantham Allee 20
53757 Sankt Augustin (Germany)
Tel: +49 (2241) 865 - 635
Fax: +49 (2241) 865 - 8635
http://www.fh-rhein-sieg.de
mailto:[EMAIL PROTECTED]

Attachment: postgresql.patch
Description: Binary data

Reply via email to