Merge pull request #2264 from KrisThielemans/tp_vectorcall_offset_Slot

[Python] Replace tp_print with tp_vectorcall_offset slot from Python 3.8
diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index f6fb3ec..6079115 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -96,7 +96,6 @@
         - SWIGLANG: octave
           CPPSTD: c++11
         - SWIGLANG: octave
-          VER: '6.4'
           CPPSTD: c++11
         - SWIGLANG: perl5
         - SWIGLANG: php
@@ -117,6 +116,7 @@
         - SWIGLANG: python
           VER: '3.2'
           os: ubuntu-18.04 # Python < 3.5 not available for 20.04.
+          continue-on-error: true # broken packages problem
         - SWIGLANG: python
           VER: '3.3'
           os: ubuntu-18.04 # Python < 3.5 not available for 20.04.
diff --git a/CHANGES.current b/CHANGES.current
index 0c6db7c..f14dc24 100644
--- a/CHANGES.current
+++ b/CHANGES.current
@@ -7,6 +7,9 @@
 Version 4.1.0 (in progress)
 ===========================
 
+2022-05-04: wsfulton
+            [C#] Add C# wchar_t * director typemaps
+
 2022-04-20: cminyard
 	    Fix an issue where newlines were not properly generated
 	    for godirectorin typemaps.  If you have a virtual function
@@ -18,6 +21,9 @@
 	    Also add an example of using goin and godirectorin and add
 	    a test for this situation.
 
+2022-04-29: jason-daly, JerryJoyce, wsfulton
+	    [C#] #1233 Add wchar_t * and std::wstring Unicode string support on Linux.
+
 2022-04-11: robinst
 	    #2257 Fix new Ruby 3.2 warning "undefining the allocator of T_DATA
 	    class swig_runtime_data".
diff --git a/Examples/test-suite/csharp/Makefile.in b/Examples/test-suite/csharp/Makefile.in
index 74010c2..600c595 100644
--- a/Examples/test-suite/csharp/Makefile.in
+++ b/Examples/test-suite/csharp/Makefile.in
@@ -25,6 +25,7 @@
 	csharp_namespace_system_collision \
 	csharp_prepost \
 	csharp_typemaps \
+	director_wstring \
 	enum_thorough_simple \
 	enum_thorough_typesafe \
 	exception_partial_info \
diff --git a/Examples/test-suite/csharp/director_wstring_runme.cs b/Examples/test-suite/csharp/director_wstring_runme.cs
new file mode 100644
index 0000000..7258e7f
--- /dev/null
+++ b/Examples/test-suite/csharp/director_wstring_runme.cs
@@ -0,0 +1,56 @@
+using System;
+using director_wstringNamespace;
+
+public class runme
+{
+    static void Main()
+    {
+        runme r = new runme();
+        r.run();
+    }
+
+    void run()
+    {
+        director_wstring_B b = new director_wstring_B("hello");
+
+        b.get(0);
+        if (b.get_first() != "hello world!")
+            throw new ApplicationException("Incorrect get_first:" + b.get_first());
+
+        b.call_process_func();
+        if (b.smem != "hello")
+            throw new ApplicationException("Incorrect smem:" + b.smem);
+
+        b.call_process_wstring_func();
+        if (b.smem != "hello (wstring)")
+            throw new ApplicationException("Incorrect smem:" + b.smem);
+
+        b.call_process_wstring_ref_func();
+        if (b.smem != "hello (wstring ref)")
+            throw new ApplicationException("Incorrect smem:" + b.smem);
+    }
+}
+
+class director_wstring_B : A
+{
+    public String smem;
+    public director_wstring_B(String first) : base(first)
+    {
+    }
+    public override String get_first()
+    {
+        return base.get_first() + " world!";
+    }
+    public override void process_text(String s)
+    {
+        this.smem = s;
+    }
+    public override void process_wstring_text(String s)
+    {
+        this.smem = s + " (wstring)";
+    }
+    public override void process_wstring_ref_text(String s)
+    {
+        this.smem = s + " (wstring ref)";
+    }
+}
diff --git a/Examples/test-suite/csharp/li_std_wstring_runme.cs b/Examples/test-suite/csharp/li_std_wstring_runme.cs
index d292728..21c225e 100644
--- a/Examples/test-suite/csharp/li_std_wstring_runme.cs
+++ b/Examples/test-suite/csharp/li_std_wstring_runme.cs
@@ -1,3 +1,6 @@
+// This file has a BOM for UTF-8
+// Notes for displaying UTF-8 properly in Windows: https://stackoverflow.com/questions/49476326/displaying-unicode-in-powershell
+
 using System;
 using li_std_wstringNamespace;
 
@@ -5,17 +8,38 @@
 {
     static private void check_equal(char a, char b)
     {
-      if (a != b)
-        throw new Exception("char failed '" + a + "' != '" + b + "'");
+        if (a != b)
+            throw new Exception("char failed '" + a + "' != '" + b + "'");
+    }
+
+    static private void display_bytes(string s)
+    {
+        Console.Write("[");
+        if (s != null)
+        {
+            foreach (char x in s)
+            {
+                int n = Convert.ToInt32(x);
+                Console.Write(n.ToString("X") + " ");
+            }
+        }
+        else
+            Console.Write("null");
+        Console.WriteLine("]");
     }
 
     static private void check_equal(string a, string b)
     {
-      if (a != b)
-        throw new Exception("string failed '" + a + "' != '" + b + "'");
+        if (li_std_wstring.debug) {
+            Console.WriteLine("check_equal {0} {1}", a, b);
+            display_bytes(a);
+            display_bytes(b);
+        }
+        if (a != b)
+          throw new Exception("string failed '" + a + "' != '" + b + "'");
     }
 
-    static void Main() 
+    static void Main()
     {
         char h = 'h';
         check_equal(li_std_wstring.test_wcvalue(h), h);
@@ -30,6 +54,8 @@
         li_std_wstring.test_pointer(null);
         li_std_wstring.test_const_pointer(null);
 
+        check_equal(li_std_wstring.test_ccvalue(null), null);
+
         try {
             li_std_wstring.test_value(null);
             throw new Exception("NULL check failed");
@@ -37,8 +63,8 @@
         }
 
         try {
-              li_std_wstring.test_reference(null);
-              throw new Exception("NULL check failed");
+            li_std_wstring.test_reference(null);
+            throw new Exception("NULL check failed");
         } catch (ArgumentNullException e) {
             if (!e.Message.Contains("type is null"))
                 throw new Exception("Missing text " + e);
@@ -54,13 +80,24 @@
         x = "hello";
         check_equal(li_std_wstring.test_const_reference(x), x);
 
-        /* Postpone, tricky, std::wstring portability problem.
+        /* Tricky, std::wstring portability problem.
          * std::wstring is 2 bytes on Windows, 4 bytes on Linux, LPWSTR is 2 bytes.
-         * .NET marshalling should work on Windows but not Linux.
-        string s = "abc";
-        if (!li_std_wstring.test_equal_abc(s))
-            throw new Exception("Not equal " + s);
-        */
+         */
+        string ss = "abc";
+        if (!li_std_wstring.test_equal_abc(ss))
+            throw new Exception("Not equal " + ss);
+
+        ss = "JP: 日本語";
+        if (!li_std_wstring.test_equal_jp(ss))
+            throw new Exception("Not equal " + ss);
+
+        ss = "DE: Kröpeliner Straße";
+        if (!li_std_wstring.test_equal_de(ss))
+            throw new Exception("Not equal " + ss);
+
+        ss = "RU: Война и мир";
+        if (!li_std_wstring.test_equal_ru(ss))
+            throw new Exception("Not equal " + ss);
 
         try {
             li_std_wstring.test_throw();
@@ -68,6 +105,24 @@
             check_equal(e.Message, "throwing test_throw");
         }
 
+        try {
+            li_std_wstring.test_throw_jp();
+        } catch (Exception e) {
+            check_equal(e.Message, "JP: 日本語");
+        }
+
+        try {
+            li_std_wstring.test_throw_ref_jp();
+        } catch (Exception e) {
+            check_equal(e.Message, "JP: 日本語");
+        }
+
+        try {
+            li_std_wstring.test_throw_wchar_t_ptr();
+        } catch (Exception e) {
+            check_equal(e.Message, "JP: 日本語");
+        }
+
         x = "abc\0def";
         // Unlike other languages, embedded NULL in std::string not supported
         // check_equal(li_std_wstring.test_value(x), x);
@@ -81,15 +136,22 @@
         check_equal(s.wchar_t_member, h);
         s.wchar_t_ptr_member = x;
         check_equal(s.wchar_t_ptr_member, "abc");
+        s.wchar_t_ptr_member = ss;
+        check_equal(s.wchar_t_ptr_member, ss);
 
         {
             // Unicode strings
+            // Strings below are UTF8 in this file, but .NET holds them internally as UTF16
+            // DE: https://www.utf8-chartable.de/
+            // RU: https://www.utf8-chartable.de/unicode-utf8-table.pl?start=1024
             string[] test_strings = {
                 "JP: 日本語", "DE: Kröpeliner Straße" , "RU: Война и мир", "EN: War and Peace"
             };
 
             foreach (string expected in test_strings)
             {
+                if (li_std_wstring.debug)
+                    Console.WriteLine("expected (C#): " + expected);
                 string received = li_std_wstring.test_value(expected);
                 check_equal(received, expected);
             }
diff --git a/Examples/test-suite/director_wstring.i b/Examples/test-suite/director_wstring.i
index 17761ea..02df9d6 100644
--- a/Examples/test-suite/director_wstring.i
+++ b/Examples/test-suite/director_wstring.i
@@ -39,10 +39,11 @@
 
   std::vector<std::wstring> m_strings;
 
+  virtual const wchar_t * wchar_out() { return L"ciao"; }
 
-  virtual void process_text(const wchar_t *text) 
-  {
-  }
+  virtual void process_text(const wchar_t *text) {}
+  virtual void process_wstring_text(std::wstring text) {}
+  virtual void process_wstring_ref_text(const std::wstring& text) {}
 
   virtual std::wstring multiple_params_val(const std::wstring& p1, const std::wstring& p2, std::wstring p3, std::wstring p4) const
   { return get_first(); }
@@ -51,6 +52,8 @@
   { return get_first(); }
   
   void call_process_func() { process_text(L"hello"); }
+  void call_process_wstring_func() { process_wstring_text(L"hello"); }
+  void call_process_wstring_ref_func() { process_wstring_ref_text(L"hello"); }
  };
  
  %}
diff --git a/Examples/test-suite/li_std_wstring.h b/Examples/test-suite/li_std_wstring.h
new file mode 100644
index 0000000..a16b7cd
--- /dev/null
+++ b/Examples/test-suite/li_std_wstring.h
@@ -0,0 +1,5 @@
+// This file has a BOM set to UTF-8, which is one way for Visual C++ to correctly interpet these strings
+// Alternatively, the /utf-8 command line option could be used
+#define JP_WSTRING L"JP: 日本語"
+#define DE_WSTRING L"DE: Kröpeliner Straße"
+#define RU_WSTRING L"RU: Война и мир"
\ No newline at end of file
diff --git a/Examples/test-suite/li_std_wstring.i b/Examples/test-suite/li_std_wstring.i
index a790ca7..4303167 100644
--- a/Examples/test-suite/li_std_wstring.i
+++ b/Examples/test-suite/li_std_wstring.i
@@ -3,44 +3,71 @@
 // The languages below are yet to provide std_wstring.i
 #if !(defined(SWIGD) || defined(SWIGGO) || defined(SWIGGUILE) || defined(SWIGJAVASCRIPT) || defined(SWIGLUA) || defined(SWIGMZSCHEME) || defined(SWIGOCAML) || defined(SWIGOCTAVE) || defined(SWIGPERL) || defined(SWIGPHP) || defined(SWIGR) || defined(SWIGSCILAB))
 
+%warnfilter(SWIGWARN_TYPEMAP_WCHARLEAK_MSG) wchar_t_const_ptr_member;  // Setting a const wchar_t * variable may leak memory.
+
 %include <std_wstring.i>
 
+
 // throw is invalid in C++17 and later, only SWIG to use it
 #define TESTCASE_THROW1(T1) throw(T1)
 %{
 #define TESTCASE_THROW1(T1)
 %}
 
+%{
+// Unicode strings are stored in li_std_wstring.h file which has the BOM appropriately set, primarily for Visual C++ to correctly interpret the wide strings
+#include "li_std_wstring.h"
+%}
+
 %inline %{
 #include <string>
+#include <iostream>
+
+bool debug = false;
+
+void show_wstring_bytes(const std::wstring &s) {
+  unsigned char *p = (unsigned char *)s.data();
+  size_t len = s.size()*sizeof(wchar_t);
+  std::wcout << L"s: " << /*s <<*/ L"[";
+  for (size_t i = 0; i<len; i++) {
+    std::wcout << std::hex << *p << L" ";
+    p++;
+  }
+  std::wcout << L"]" << std::endl;
+  std::wcout << std::flush;
+}
 
 wchar_t test_wcvalue(wchar_t x) {
-   return x;
+  return x;
 }
 
 const wchar_t* test_ccvalue(const wchar_t* x) {
-   return x;
+  return x;
 }
 
 wchar_t* test_cvalue(wchar_t* x) {
-   return x;
+  return x;
 }
   
 
 wchar_t* test_wchar_overload() {
-   return 0;
+  return 0;
 }
 
 wchar_t* test_wchar_overload(wchar_t *x) {
-   return x;
+  return x;
 }
 
 std::wstring test_value(std::wstring x) {
-   return x;
+  if (debug) {
+    std::wcout << "received(C++): " /*<< x */<< std::endl;
+    show_wstring_bytes(x);
+  }
+  return x;
 }
 
 const std::wstring& test_const_reference(const std::wstring &x) {
-   return x;
+  return x;
 }
 
 void test_pointer(std::wstring *x) {
@@ -52,13 +79,47 @@
 void test_reference(std::wstring &x) {
 }
 
-bool test_equal_abc(const std::wstring &s) {
-  return L"abc" == s;
+bool test_equal(const wchar_t *wcs, const std::wstring& s) {
+  if (debug) {
+    show_wstring_bytes(wcs);
+    show_wstring_bytes(s);
+  }
+  return wcs == s;
 }
 
-void test_throw() TESTCASE_THROW1(std::wstring){
+bool test_equal_abc(const std::wstring &s) {
+  return test_equal(L"abc", s);
+}
+
+bool test_equal_jp(const std::wstring &s) {
+  return test_equal(JP_WSTRING, s);
+}
+
+bool test_equal_de(const std::wstring &s) {
+  return test_equal(DE_WSTRING, s);
+}
+
+bool test_equal_ru(const std::wstring &s) {
+  return test_equal(RU_WSTRING, s);
+}
+
+void test_throw() TESTCASE_THROW1(std::wstring) {
   static std::wstring x = L"throwing test_throw";
-  
+  throw x;
+}
+
+void test_throw_wchar_t_ptr() TESTCASE_THROW1(std::wstring) {
+  static std::wstring x = JP_WSTRING;
+  throw x;
+}
+
+void test_throw_jp() TESTCASE_THROW1(std::wstring) {
+  static std::wstring x = JP_WSTRING;
+  throw x;
+}
+
+void test_throw_ref_jp() TESTCASE_THROW1(const std::wstring&) {
+  static std::wstring x = JP_WSTRING;
   throw x;
 }
 
@@ -73,6 +134,8 @@
 struct wchar_test_struct {
   wchar_t wchar_t_member;
   wchar_t* wchar_t_ptr_member;
+  const wchar_t* wchar_t_const_ptr_member;
+  wchar_test_struct() : wchar_t_member(), wchar_t_ptr_member(), wchar_t_const_ptr_member() {}
 };
 
 %}
diff --git a/Examples/test-suite/python/director_wstring_runme.py b/Examples/test-suite/python/director_wstring_runme.py
index 5facc1f..659cf18 100644
--- a/Examples/test-suite/python/director_wstring_runme.py
+++ b/Examples/test-suite/python/director_wstring_runme.py
@@ -12,6 +12,12 @@
     def process_text(self, s):
         self.smem = s
 
+    def process_wstring_text(self, s):
+        self.smem = s + " (wstring)"
+
+    def process_wstring_ref_text(self, s):
+        self.smem = s + " (wstring ref)"
+
 
 b = B("hello")
 
@@ -24,3 +30,13 @@
 
 if b.smem != "hello":
     raise RuntimeError("smem: {}".format(smem))
+
+b.call_process_wstring_func()
+
+if b.smem != "hello (wstring)":
+    raise RuntimeError("smem: {}".format(smem))
+
+b.call_process_wstring_ref_func()
+
+if b.smem != "hello (wstring ref)":
+    raise RuntimeError("smem: {}".format(smem))
diff --git a/Examples/test-suite/ruby/li_std_set_runme.rb b/Examples/test-suite/ruby/li_std_set_runme.rb
index efc163b..455a170 100644
--- a/Examples/test-suite/ruby/li_std_set_runme.rb
+++ b/Examples/test-suite/ruby/li_std_set_runme.rb
@@ -56,11 +56,15 @@
 s = LanguageSet.new
 s.insert([1,2])
 s.insert(1)
-s.insert("hello")
+# There is a reference count issue that needs fixing, see https://github.com/swig/swig/issues/2115
+# Workaround is to create hello variable containing a string and use it instead of just "hello"
+hello = "hello"
+s.insert(hello)
 #s.to_a == [1,[1,2],'hello']  # sort order: s.sort {|a,b| a.hash <=> b.hash}
 # Test above is flawed as LanguageSet sorts by each element's hash, so the order will change from one invocation to the next. Sort a conversion to array instead.
+GC.start
 sa = s.to_a.sort { |x, y| x.to_s <=> y.to_s }
-sa == [1,[1,2],'hello']
+sa == [1,[1,2],hello]
 
 EOF
 
diff --git a/Lib/csharp/std_wstring.i b/Lib/csharp/std_wstring.i
index 162b90e..1d10ca8 100644
--- a/Lib/csharp/std_wstring.i
+++ b/Lib/csharp/std_wstring.i
@@ -2,7 +2,9 @@
  * std_wstring.i
  *
  * Typemaps for std::wstring and const std::wstring&
- * These are mapped to a C# String and are passed around by value.
+ * std::wstring is mapped to a C# Unicode string (UTF16) and is passed around by value.
+ * std::wstring support includes wchar_t as a 2 byte type (Windows) and a 4 byte type
+ * (most Unix systems).
  *
  * To use non-const std::wstring references use the following %apply.  Note 
  * that they are passed by value.
@@ -15,6 +17,28 @@
 #include <string>
 %}
 
+%fragment("Swig_csharp_UTF16ToWString", "header") %{
+/* For converting from .NET UTF16 (2 byte unicode) strings. wchar_t is 2 bytes on Windows, 4 bytes on Linux. */
+static std::wstring Swig_csharp_UTF16ToWString(const unsigned short *str) {
+  if (sizeof(wchar_t) == 2) {
+    return std::wstring((wchar_t *)str);
+  } else {
+    const unsigned short *pBegin(str);
+    const unsigned short *ptr(pBegin);
+
+    while (*ptr != 0)
+      ++ptr;
+
+    std::wstring result;
+    result.reserve(ptr - pBegin);
+    while(pBegin != ptr)
+      result.push_back(*pBegin++);
+
+    return result;
+  }
+}
+%}
+
 namespace std {
 
 %naturalvar wstring;
@@ -22,31 +46,33 @@
 class wstring;
 
 // wstring
-%typemap(ctype, out="void *") wstring "wchar_t *"
+%typemap(ctype, out="void *") wstring "unsigned short *"
 %typemap(imtype,
          inattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
-         outattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]"
+         outattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
+         directorinattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
+         directoroutattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]"
          ) wstring "string"
 %typemap(cstype) wstring "string"
 %typemap(csdirectorin) wstring "$iminput"
 %typemap(csdirectorout) wstring "$cscall"
 
-%typemap(in, canthrow=1) wstring 
+%typemap(in, canthrow=1, fragment="Swig_csharp_UTF16ToWString") wstring
 %{ if (!$input) {
     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0);
     return $null;
    }
-   $1.assign($input); %}
-%typemap(out) wstring %{ $result = SWIG_csharp_wstring_callback($1.c_str()); %}
+   $1 = Swig_csharp_UTF16ToWString($input); %}
+%typemap(out) wstring %{ $result = SWIG_csharp_wstring_with_length_callback($1.c_str(), (int)$1.size()); %}
 
-%typemap(directorout, canthrow=1) wstring 
+%typemap(directorout, canthrow=1) wstring
 %{ if (!$input) {
     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0);
     return $null;
    }
-   $result.assign($input); %}
+   $result = Swig_csharp_UTF16ToWString($input); %}
 
-%typemap(directorin) wstring %{ $input = SWIG_csharp_wstring_callback($1.c_str()); %}
+%typemap(directorin) wstring %{ $input = SWIG_csharp_wstring_with_length_callback($1.c_str(), (int)$1.size()); %}
 
 %typemap(csin) wstring "$csinput"
 %typemap(csout, excode=SWIGEXCODE) wstring {
@@ -57,29 +83,30 @@
 %typemap(typecheck) wstring = wchar_t *;
 
 %typemap(throws, canthrow=1) wstring
-%{ std::string message($1.begin(), $1.end());
-   SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, message.c_str());
+%{ SWIG_csharp_ApplicationException_callback($1.c_str(), (int)$1.size());
    return $null; %}
 
 // const wstring &
-%typemap(ctype, out="void *") const wstring & "wchar_t *"
+%typemap(ctype, out="void *") const wstring & "unsigned short *"
 %typemap(imtype,
          inattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
-         outattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]"
+         outattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
+         directorinattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
+         directoroutattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]"
          ) const wstring & "string"
 %typemap(cstype) const wstring & "string"
 
 %typemap(csdirectorin) const wstring & "$iminput"
 %typemap(csdirectorout) const wstring & "$cscall"
 
-%typemap(in, canthrow=1) const wstring &
+%typemap(in, canthrow=1, fragment="Swig_csharp_UTF16ToWString") const wstring &
 %{ if (!$input) {
     SWIG_CSharpSetPendingExceptionArgument(SWIG_CSharpArgumentNullException, "null wstring", 0);
     return $null;
    }
-   std::wstring $1_str($input);
+   std::wstring $1_str(Swig_csharp_UTF16ToWString($input));
    $1 = &$1_str; %}
-%typemap(out) const wstring & %{ $result = SWIG_csharp_wstring_callback($1->c_str()); %}
+%typemap(out) const wstring & %{ $result = SWIG_csharp_wstring_with_length_callback($1->c_str(), (int)$1->size()); %}
 
 %typemap(csin) const wstring & "$csinput"
 %typemap(csout, excode=SWIGEXCODE) const wstring & {
@@ -94,10 +121,10 @@
    }
    /* possible thread/reentrant code problem */
    static std::wstring $1_str;
-   $1_str = $input;
+   $1_str = Swig_csharp_UTF16ToWString($input);
    $result = &$1_str; %}
 
-%typemap(directorin) const wstring & %{ $input = SWIG_csharp_wstring_callback($1.c_str()); %}
+%typemap(directorin) const wstring & %{ $input = SWIG_csharp_wstring_with_length_callback($1.c_str(), (int)$1.size()); %}
 
 %typemap(csvarin, excode=SWIGEXCODE2) const wstring & %{
     set {
@@ -112,8 +139,7 @@
 %typemap(typecheck) const wstring & = wchar_t *;
 
 %typemap(throws, canthrow=1) const wstring &
-%{ std::string message($1.begin(), $1.end());
-   SWIG_CSharpSetPendingException(SWIG_CSharpApplicationException, message.c_str());
+%{ SWIG_csharp_ApplicationException_callback($1.c_str(), (int)$1.size());
    return $null; %}
 
 }
diff --git a/Lib/csharp/wchar.i b/Lib/csharp/wchar.i
index 1ece767..2ada4df 100644
--- a/Lib/csharp/wchar.i
+++ b/Lib/csharp/wchar.i
@@ -2,37 +2,59 @@
  * wchar.i
  *
  * Typemaps for the wchar_t type
- * These are mapped to a C# String and are passed around by value.
+ * wchar_t * is mapped to a C# Unicode string (UTF16) and is passed around by value.
+ * wchar_t * support includes wchar_t as a 2 byte type (Windows) and a 4 byte type
+ * (most Unix systems).
  *
  * Support code for wide strings can be turned off by defining SWIG_CSHARP_NO_WSTRING_HELPER
- *
  * ----------------------------------------------------------------------------- */
 
 #if !defined(SWIG_CSHARP_NO_WSTRING_HELPER)
 #if !defined(SWIG_CSHARP_WSTRING_HELPER_)
 #define SWIG_CSHARP_WSTRING_HELPER_
+
+%fragment("<wchar.h>"); // TODO: %fragment("<wchar.h", "runtime");
+
 %insert(runtime) %{
 /* Callback for returning strings to C# without leaking memory */
-typedef void * (SWIGSTDCALL* SWIG_CSharpWStringHelperCallback)(const wchar_t *);
-static SWIG_CSharpWStringHelperCallback SWIG_csharp_wstring_callback = NULL;
+typedef void * (SWIGSTDCALL* SWIG_CSharpWStringHelperCallback)(const wchar_t *, int length);
+static SWIG_CSharpWStringHelperCallback SWIG_csharp_wstring_with_length_callback = NULL;
 %}
 
+%insert(header) %{
+static void * SWIG_csharp_wstring_callback(const wchar_t *s) {
+  return SWIG_csharp_wstring_with_length_callback(s, (int)wcslen(s));
+}
+%}
+
+
 %pragma(csharp) imclasscode=%{
   protected class SWIGWStringHelper {
 
     [return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]
-    public delegate string SWIGWStringDelegate(global::System.IntPtr message);
-    static SWIGWStringDelegate wstringDelegate = new SWIGWStringDelegate(CreateWString);
+    public delegate string SWIGWStringDelegate(global::System.IntPtr message, int length);
+    static SWIGWStringDelegate wstringUTF16Delegate = new SWIGWStringDelegate(CreateWStringFromUTF16);
+    static SWIGWStringDelegate wstringUTF32Delegate = new SWIGWStringDelegate(CreateWStringFromUTF32);
 
     [global::System.Runtime.InteropServices.DllImport("$dllimport", EntryPoint="SWIGRegisterWStringCallback_$module")]
-    public static extern void SWIGRegisterWStringCallback_$module(SWIGWStringDelegate wstringDelegate);
+    public static extern void SWIGRegisterWStringCallback_$module(SWIGWStringDelegate wstringUTF16Delegate, SWIGWStringDelegate wstringUTF32Delegate);
 
-    static string CreateWString([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString) {
-      return global::System.Runtime.InteropServices.Marshal.PtrToStringUni(cString);
+    public static string CreateWStringFromUTF16([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString, int length) {
+      return global::System.Runtime.InteropServices.Marshal.PtrToStringUni(cString, length);
+    }
+
+    public static string CreateWStringFromUTF32([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString, int length) {
+      if (length == 0)
+        return string.Empty;
+
+      byte[] buffer = new byte[length * 4];
+      global::System.Runtime.InteropServices.Marshal.Copy(cString, buffer, 0, buffer.Length);
+      byte[] utf8buffer = global::System.Text.Encoding.Convert(global::System.Text.Encoding.UTF32, global::System.Text.Encoding.UTF8, buffer);
+      return global::System.Text.Encoding.Default.GetString(utf8buffer);
     }
 
     static SWIGWStringHelper() {
-      SWIGRegisterWStringCallback_$module(wstringDelegate);
+      SWIGRegisterWStringCallback_$module(wstringUTF16Delegate, wstringUTF32Delegate);
     }
   }
 
@@ -43,13 +65,78 @@
 #ifdef __cplusplus
 extern "C"
 #endif
-SWIGEXPORT void SWIGSTDCALL SWIGRegisterWStringCallback_$module(SWIG_CSharpWStringHelperCallback callback) {
-  SWIG_csharp_wstring_callback = callback;
+SWIGEXPORT void SWIGSTDCALL SWIGRegisterWStringCallback_$module(SWIG_CSharpWStringHelperCallback callback_utf16, SWIG_CSharpWStringHelperCallback callback_utf32) {
+  SWIG_csharp_wstring_with_length_callback = sizeof(wchar_t) == 2 ? callback_utf16 : callback_utf32;
 }
 %}
 #endif // SWIG_CSHARP_WSTRING_HELPER_
 #endif // SWIG_CSHARP_NO_WSTRING_HELPER
 
+#if !defined(SWIG_CSHARP_NO_WSTRING_EXCEPTION_HELPER)
+#if !defined(SWIG_CSHARP_WSTRING_EXCEPTION_HELPER_)
+#define SWIG_CSHARP_WSTRING_EXCEPTION_HELPER_
+
+%insert(runtime) %{
+/* Callback for returning strings to C# without leaking memory */
+typedef void (SWIGSTDCALL* SWIG_CSharpWStringExceptionHelperCallback)(const wchar_t *, int length);
+static SWIG_CSharpWStringExceptionHelperCallback SWIG_csharp_ApplicationException_callback = NULL;
+%}
+
+%pragma(csharp) imclasscode=%{
+  protected class SWIGWStringExceptionHelper {
+
+    [return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]
+    public delegate void SWIGWStringExceptionDelegate(global::System.IntPtr message, int length);
+    static SWIGWStringExceptionDelegate applicationExceptionUTF16Delegate = new SWIGWStringExceptionDelegate(SetPendingApplicationExceptionUTF16);
+    static SWIGWStringExceptionDelegate applicationExceptionUTF32Delegate = new SWIGWStringExceptionDelegate(SetPendingApplicationExceptionUTF32);
+
+    [global::System.Runtime.InteropServices.DllImport("$dllimport", EntryPoint="SWIGRegisterWStringExceptionCallback_$module")]
+    public static extern void SWIGRegisterWStringExceptionCallback_$module(SWIGWStringExceptionDelegate applicationExceptionUTF16Delegate, SWIGWStringExceptionDelegate applicationExceptionUTF32Delegate);
+
+    static string CreateWStringFromUTF16([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString, int length) {
+      return global::System.Runtime.InteropServices.Marshal.PtrToStringUni(cString, length);
+    }
+
+    public static string CreateWStringFromUTF32([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString, int length) {
+      if (length == 0)
+        return string.Empty;
+
+      byte[] buffer = new byte[length * 4];
+      global::System.Runtime.InteropServices.Marshal.Copy(cString, buffer, 0, buffer.Length);
+      byte[] utf8buffer = global::System.Text.Encoding.Convert(global::System.Text.Encoding.UTF32, global::System.Text.Encoding.UTF8, buffer);
+      return global::System.Text.Encoding.Default.GetString(utf8buffer);
+    }
+
+    static void SetPendingApplicationExceptionUTF16([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString, int length) {
+      string message = SWIGWStringHelper.CreateWStringFromUTF16(cString, length);
+      SWIGPendingException.Set(new global::System.ApplicationException(message, SWIGPendingException.Retrieve()));
+    }
+
+    static void SetPendingApplicationExceptionUTF32([global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]global::System.IntPtr cString, int length) {
+      string message = SWIGWStringHelper.CreateWStringFromUTF32(cString, length);
+      SWIGPendingException.Set(new global::System.ApplicationException(message, SWIGPendingException.Retrieve()));
+    }
+
+    static SWIGWStringExceptionHelper() {
+      SWIGRegisterWStringExceptionCallback_$module(applicationExceptionUTF16Delegate, applicationExceptionUTF32Delegate);
+    }
+  }
+
+  static protected SWIGWStringExceptionHelper swigWStringExceptionHelper = new SWIGWStringExceptionHelper();
+%}
+
+%insert(runtime) %{
+#ifdef __cplusplus
+extern "C"
+#endif
+SWIGEXPORT void SWIGSTDCALL SWIGRegisterWStringExceptionCallback_$module(SWIG_CSharpWStringExceptionHelperCallback callback_utf16, SWIG_CSharpWStringExceptionHelperCallback callback_utf32) {
+  SWIG_csharp_ApplicationException_callback = sizeof(wchar_t) == 2 ? callback_utf16 : callback_utf32;
+}
+%}
+
+#endif // SWIG_CSHARP_WSTRING_EXCEPTION_HELPER_
+#endif // SWIG_CSHARP_NO_WSTRING_EXCEPTION_HELPER
+
 
 // wchar_t
 %typemap(ctype) wchar_t "wchar_t"
@@ -77,13 +164,64 @@
 %typemap(typecheck) wchar_t = char;
 
 // wchar_t *
-%typemap(ctype) wchar_t * "wchar_t *"
-%typemap(imtype, inattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]", out="global::System.IntPtr" ) wchar_t * "string"
+
+%fragment("Swig_csharp_UTF16ToWCharPtr", "header") %{
+/* For converting from .NET UTF16 (2 byte unicode) strings. wchar_t is 2 bytes on Windows, 4 bytes on Linux. */
+static wchar_t * Swig_csharp_UTF16ToWCharPtr(const unsigned short *str) {
+  if (sizeof(wchar_t) == 2) {
+    return (wchar_t *)str;
+  } else {
+    wchar_t *result = 0;
+
+    if (str) {
+      const unsigned short *pBegin(str);
+      const unsigned short *pEnd(pBegin);
+      wchar_t *ptr = 0;
+
+      while (*pEnd != 0)
+        ++pEnd;
+
+#ifdef __cplusplus
+      result = ptr = new wchar_t[pEnd - pBegin + 1];
+#else
+      result = ptr = (wchar_t *)malloc(sizeof(wchar_t) * (pEnd - pBegin + 1));
+#endif
+      while(pBegin != pEnd)
+        *ptr++ = *pBegin++;
+      *ptr++ = 0;
+    }
+
+    return result;
+  }
+}
+%}
+
+%fragment("Swig_csharp_UTF16ToWCharPtrFree", "header") %{
+static void Swig_csharp_UTF16ToWCharPtrFree(wchar_t *str) {
+  if (sizeof(wchar_t) != 2) {
+#ifdef __cplusplus
+    delete [] str;
+#else
+    free(str);
+#endif
+  }
+}
+%}
+
+%typemap(ctype, out="void *") wchar_t * "unsigned short *"
+%typemap(imtype,
+         inattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
+         outattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
+         directorinattributes="[global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]",
+         directoroutattributes="[return: global::System.Runtime.InteropServices.MarshalAs(global::System.Runtime.InteropServices.UnmanagedType.LPWStr)]"
+         ) wchar_t * "string"
 %typemap(cstype) wchar_t * "string"
+%typemap(csdirectorin) wchar_t * "$iminput"
+%typemap(csdirectorout) wchar_t * "$cscall"
 
 %typemap(csin) wchar_t * "$csinput"
 %typemap(csout, excode=SWIGEXCODE) wchar_t * {
-    string ret = global::System.Runtime.InteropServices.Marshal.PtrToStringUni($imcall);$excode
+    string ret = $imcall;$excode
     return ret;
   }
 %typemap(csvarin, excode=SWIGEXCODE2) wchar_t * %{
@@ -92,12 +230,109 @@
     } %}
 %typemap(csvarout, excode=SWIGEXCODE2) wchar_t * %{
     get {
-      string ret = global::System.Runtime.InteropServices.Marshal.PtrToStringUni($imcall);$excode
+      string ret = $imcall;$excode
       return ret;
     } %}
 
-%typemap(in) wchar_t * %{ $1 = ($1_ltype)$input; %}
-%typemap(out) wchar_t * %{ $result = (wchar_t *)$1; %}
+%typemap(in, fragment="Swig_csharp_UTF16ToWCharPtr") wchar_t *
+%{ $1 = Swig_csharp_UTF16ToWCharPtr($input); %}
+
+%typemap(out) wchar_t * %{ $result = $1 ? SWIG_csharp_wstring_callback($1) : 0; %}
+
+%typemap(freearg, fragment="Swig_csharp_UTF16ToWCharPtrFree") wchar_t *
+%{ Swig_csharp_UTF16ToWCharPtrFree($1); %}
+
+%typemap(directorout, warning=SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG) wchar_t *
+%{ $result = Swig_csharp_UTF16ToWCharPtr($input); %}
+
+%typemap(directorin) wchar_t * %{ $input = SWIG_csharp_wstring_with_length_callback($1, (int)wcslen($1)); %}
 
 %typemap(typecheck) wchar_t * = char *;
 
+%typemap(throws, canthrow=1, fragment="<wchar.h>") wchar_t *
+%{ SWIG_csharp_ApplicationException_callback($1, (int)wcslen($1));
+   return $null; %}
+
+
+/* Default typemap for handling wchar_t * members (based on char * in swig.swg) */
+
+#ifdef __cplusplus
+%typemap(memberin,fragment="<wchar.h>") wchar_t * {
+  delete [] $1;
+  if ($input && sizeof(wchar_t) == 2) {
+    $1 = ($1_type) (new wchar_t[wcslen((const wchar_t *)$input)+1]);
+    wcscpy((wchar_t *)$1, (const wchar_t *)$input);
+  } else {
+    $1 = $input;
+    $input = 0;
+  }
+}
+%typemap(memberin,warning=SWIGWARN_TYPEMAP_WCHARLEAK_MSG,fragment="<wchar.h>") const wchar_t * {
+  if ($input && sizeof(wchar_t) == 2) {
+    $1 = ($1_type) (new wchar_t[wcslen((const wchar_t *)$input)+1]);
+    wcscpy((wchar_t *)$1, (const wchar_t *)$input);
+  } else {
+    $1 = $input;
+    $input = 0;
+  }
+}
+%typemap(globalin,fragment="<wchar.h>") wchar_t * {
+  delete [] $1;
+  if ($input && sizeof(wchar_t) == 2) {
+    $1 = ($1_type) (new wchar_t[wcslen((const wchar_t *)$input)+1]);
+    wcscpy((wchar_t *)$1, (const wchar_t *)$input);
+  } else {
+    $1 = $input;
+    $input = 0;
+  }
+}
+%typemap(globalin,warning=SWIGWARN_TYPEMAP_WCHARLEAK_MSG,fragment="<wchar.h>") const wchar_t * {
+  if ($input && sizeof(wchar_t) == 2) {
+    $1 = ($1_type) (new wchar_t[wcslen((const wchar_t *)$input)+1]);
+    wcscpy((wchar_t *)$1, (const wchar_t *)$input);
+  } else {
+    $1 = $input;
+    $input = 0;
+  }
+}
+#else
+%typemap(memberin,fragment="<wchar.h>") wchar_t * {
+  free($1);
+  if ($input && sizeof(wchar_t) == 2) {
+    $1 = ($1_type) malloc(wcslen((const wchar_t *)$input)+1);
+    wcscpy((wchar_t *)$1, (const wchar_t *)$input);
+  } else {
+    $1 = $input;
+    $input = 0;
+  }
+}
+%typemap(memberin,warning=SWIGWARN_TYPEMAP_WCHARLEAK_MSG,fragment="<wchar.h>") const wchar_t * {
+  if ($input && sizeof(wchar_t) == 2) {
+    $1 = ($1_type) malloc(wcslen((const wchar_t *)$input)+1);
+    wcscpy((wchar_t *)$1, (const wchar_t *)$input);
+  } else {
+    $1 = $input;
+    $input = 0;
+  }
+}
+%typemap(globalin,fragment="<wchar.h>") wchar_t * {
+  free($1);
+  if ($input && sizeof(wchar_t) == 2) {
+    $1 = ($1_type) malloc(wcslen((const wchar_t *)$input)+1);
+    wcscpy((wchar_t *)$1, (const wchar_t *)$input);
+  } else {
+    $1 = $input;
+    $input = 0;
+  }
+}
+%typemap(globalin,warning=SWIGWARN_TYPEMAP_WCHARLEAK_MSG,fragment="<wchar.h>") const wchar_t * {
+  if ($input && sizeof(wchar_t) == 2) {
+    $1 = ($1_type) malloc(wcslen((const wchar_t *)$input)+1);
+    wcscpy((wchar_t *)$1, (const wchar_t *)$input);
+  } else {
+    $1 = $input;
+    $input = 0;
+  }
+}
+
+#endif
diff --git a/Lib/javascript/jsc/javascriptstrings.swg b/Lib/javascript/jsc/javascriptstrings.swg
index 55c8e4b..5c8081a 100644
--- a/Lib/javascript/jsc/javascriptstrings.swg
+++ b/Lib/javascript/jsc/javascriptstrings.swg
@@ -75,6 +75,7 @@
 }
 
 %define %_typemap2_string(StringCode, CharCode,
+			 WarningLeakMsg,
 			 Char, CharName,
 			 SWIG_AsCharPtrAndSize,
 			 SWIG_FromCharPtrAndSize,
@@ -166,6 +167,7 @@
 
 %_typemap_string(StringCode,
                  Char,
+                 WarningLeakMsg,
                  SWIG_AsCharPtrAndSize,
                  SWIG_FromCharPtrAndSize,
                  SWIG_CharPtrLen,
diff --git a/Lib/swigwarnings.swg b/Lib/swigwarnings.swg
index 34c98fb..3f9ddcb 100644
--- a/Lib/swigwarnings.swg
+++ b/Lib/swigwarnings.swg
@@ -52,6 +52,7 @@
 
 %define SWIGWARN_TYPEMAP_CHARLEAK_MSG         "451:Setting a const char * variable may leak memory." %enddef
 %define SWIGWARN_TYPEMAP_SWIGTYPELEAK_MSG     "454:Setting a pointer/reference variable may leak memory." %enddef
+%define SWIGWARN_TYPEMAP_WCHARLEAK_MSG        "455:Setting a const wchar_t * variable may leak memory." %enddef
 %define SWIGWARN_TYPEMAP_THREAD_UNSAFE_MSG    "470:Thread/reentrant unsafe wrapping, consider returning by value instead." %enddef
 %define SWIGWARN_TYPEMAP_DIRECTOROUT_PTR_MSG  "473:Returning a pointer or reference in a director method is not recommended." %enddef
 %define SWIGWARN_TYPEMAP_INITIALIZER_LIST_MSG "476:Initialization using std::initializer_list." %enddef
diff --git a/Lib/typemaps/string.swg b/Lib/typemaps/string.swg
index 4b70723..72f4aa5 100644
--- a/Lib/typemaps/string.swg
+++ b/Lib/typemaps/string.swg
@@ -30,6 +30,7 @@
 
 %include <typemaps/strings.swg>
 %typemaps_string(%checkcode(STRING), %checkcode(CHAR),
+		 SWIGWARN_TYPEMAP_CHARLEAK_MSG,
 		 char, Char, SWIG_AsCharPtrAndSize, SWIG_FromCharPtrAndSize,
 		 strlen, SWIG_strnlen,
 		"<limits.h>", CHAR_MIN, CHAR_MAX)
diff --git a/Lib/typemaps/strings.swg b/Lib/typemaps/strings.swg
index 87e97dd..e8ed401 100644
--- a/Lib/typemaps/strings.swg
+++ b/Lib/typemaps/strings.swg
@@ -19,6 +19,7 @@
 
 %define %_typemap_string(StringCode, 
 			 Char,
+			 WarningLeakMsg,
 			 SWIG_AsCharPtrAndSize,
 			 SWIG_FromCharPtrAndSize,
 			 SWIG_CharPtrLen,
@@ -86,7 +87,7 @@
   }
 }
 
-%typemap(varin,fragment=#SWIG_AsCharPtrAndSize,warning=SWIGWARN_TYPEMAP_CHARLEAK_MSG) const Char * {
+%typemap(varin,fragment=#SWIG_AsCharPtrAndSize,warning=WarningLeakMsg) const Char * {
   Char *cptr = 0; size_t csize = 0; int alloc = SWIG_NEWOBJ;
   int res = SWIG_AsCharPtrAndSize($input, &cptr, &csize, &alloc);
   if (!SWIG_IsOK(res)) {
@@ -117,7 +118,7 @@
   }
 }
 
-%typemap(memberin,noblock=1,warning=SWIGWARN_TYPEMAP_CHARLEAK_MSG) const Char * {
+%typemap(memberin,noblock=1,warning=WarningLeakMsg) const Char * {
   if ($input) {
     size_t size = SWIG_CharPtrLen(%reinterpret_cast(%reinterpret_cast($input, const Char *), const Char *)) + 1;
     $1 = ($1_type)SWIG_NewCopyCharArray($input, size, Char);
@@ -138,7 +139,7 @@
   }
 }
 
-%typemap(globalin,noblock=1,warning=SWIGWARN_TYPEMAP_CHARLEAK_MSG) const Char * {
+%typemap(globalin,noblock=1,warning=WarningLeakMsg) const Char * {
   if ($input) {
     size_t size = SWIG_CharPtrLen($input) + 1;
     $1 = ($1_type)SWIG_NewCopyCharArray($input, size, Char);
@@ -501,6 +502,7 @@
 
 #ifndef %_typemap2_string
 %define %_typemap2_string(StringCode, CharCode,
+			 WarningLeakMsg,
 			 Char, CharName,
 			 SWIG_AsCharPtrAndSize,
 			 SWIG_FromCharPtrAndSize,
@@ -591,6 +593,7 @@
 
 %_typemap_string(StringCode, 
 		 Char,
+		 WarningLeakMsg,
 		 SWIG_AsCharPtrAndSize,
 		 SWIG_FromCharPtrAndSize,
 		 SWIG_CharPtrLen,
@@ -609,6 +612,7 @@
  * ------------------------------------------------------------ */
 
 %define %typemaps_string(StringCode, CharCode,
+			 WarningLeakMsg,
 			 Char, CharName,
 			 SWIG_AsCharPtrAndSize,
 			 SWIG_FromCharPtrAndSize,
@@ -616,6 +620,7 @@
 			 SWIG_CharBufLen,
 			 FragLimits, CHAR_MIN, CHAR_MAX)
 %_typemap2_string(StringCode, CharCode,
+		  WarningLeakMsg,
 		  Char, CharName,
 		  SWIG_AsCharPtrAndSize,
 		  SWIG_FromCharPtrAndSize,
@@ -631,6 +636,7 @@
  * ------------------------------------------------------------ */
 
 %define %typemaps_string_alloc(StringCode, CharCode,
+			       WarningLeakMsg,
 			       Char, CharName,
 			       SWIG_AsCharPtrAndSize,
 			       SWIG_FromCharPtrAndSize,
@@ -640,6 +646,7 @@
 			       SWIG_DeleteCharArray,
 			       FragLimits, CHAR_MIN, CHAR_MAX)
 %_typemap2_string(StringCode, CharCode,
+		  WarningLeakMsg,
 		  Char, CharName,
 		  SWIG_AsCharPtrAndSize,
 		  SWIG_FromCharPtrAndSize,
diff --git a/Lib/typemaps/wstring.swg b/Lib/typemaps/wstring.swg
index cd409d1..d99c0bb 100644
--- a/Lib/typemaps/wstring.swg
+++ b/Lib/typemaps/wstring.swg
@@ -31,6 +31,7 @@
 
 %include <typemaps/strings.swg>
 %typemaps_string(%checkcode(UNISTRING), %checkcode(UNICHAR),
+		 SWIGWARN_TYPEMAP_WCHARLEAK_MSG,
 		 wchar_t, WChar, SWIG_AsWCharPtrAndSize, SWIG_FromWCharPtrAndSize,
 		 wcslen, SWIG_wcsnlen,
 		"<wchar.h>", WCHAR_MIN, WCHAR_MAX)
diff --git a/Source/Include/swigwarn.h b/Source/Include/swigwarn.h
index 267b69c..8755f2a 100644
--- a/Source/Include/swigwarn.h
+++ b/Source/Include/swigwarn.h
@@ -163,6 +163,7 @@
 #define WARN_TYPEMAP_SWIGTYPE         452 /* No longer issued */
 #define WARN_TYPEMAP_APPLY_UNDEF      453
 #define WARN_TYPEMAP_SWIGTYPELEAK     454
+#define WARN_TYPEMAP_WCHARLEAK        455
 
 #define WARN_TYPEMAP_IN_UNDEF         460
 #define WARN_TYPEMAP_OUT_UNDEF        461