diff --git a/include/CppSharp.h b/include/CppSharp.h index 35869a07..5dd9cf29 100644 --- a/include/CppSharp.h +++ b/include/CppSharp.h @@ -108,7 +108,10 @@ namespace clix { typedef typename StringTypeSelector::Type SourceStringType; size_t byteCount = cxxString.length() * sizeof(SourceStringType::value_type); - if(byteCount == 0) return System::String::Empty; + // Empty strings would cause trouble accessing the array below + if(byteCount == 0) { + return System::String::Empty; + } // Copy the C++ string contents into a managed array of bytes array ^bytes = gcnew array(byteCount); @@ -143,11 +146,14 @@ namespace clix { ) { typedef typename StringTypeSelector::Type StringType; + // Empty strings would cause a problem when accessing the empty managed array + if(!string || string->Length == 0) { + return StringType(); + } + // First, we use .NET's encoding classes to convert the string into a byte array array ^bytes = encode(string); - if(bytes->Length == 0) return StringType(); - // Then we construct our native string from that byte array pin_ptr pinnedBytes(&bytes[0]); return StringType(