Bug 7980 - Marshalling problem in calling a native shared library(.so) with StringBuilder which Length is not zero
Summary: Marshalling problem in calling a native shared library(.so) with StringBuilde...
Status: RESOLVED FIXED
Alias: None
Product: Runtime
Classification: Mono
Component: Interop ()
Version: unspecified
Hardware: Macintosh Mac OS
: --- normal
Target Milestone: ---
Assignee: Bugzilla
URL:
Depends on:
Blocks:
 
Reported: 2012-10-25 00:22 UTC by pebble
Modified: 2012-10-25 22:15 UTC (History)
3 users (show)

Tags:
Is this bug a regression?: ---
Last known good build:

Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report on GitHub or Developer Community with your current version information, steps to reproduce, and relevant error messages or log files if you are hitting an issue that looks similar to this resolved bug and you do not yet see a matching new report.

Related Links:
Status:
RESOLVED FIXED

Description pebble 2012-10-25 00:22:21 UTC
[makefile]
  1 CC=gcc
  2 CFLAGS=-Wall -shared -arch i386
  3 TARGET=libmoon.so
  4 all: $(TARGET)
  5     @$(CC) $(CFLAGS) getsomething.c -o main.out ./$(TARGET)
  6 $(TARGET): getsomething.c
  7     @$(CC) $(CFLAGS) -o $(TARGET) getsomething.c
  8 clean:
  9     @rm -fr *.out *.so *.o

[C source code]
  4 void getsomething( char* p, int size )
  5 {
  6     static int count = 0;
  7     if( size == 0 ) {
  8         return;
  9     }
 10     memset( p, 0, size );
 11     if( (count % 2) == 0 ){
 12         strcpy( p, "sun" );
 13     } else {
 14         strcpy( p, "moon" );
 15     }
 16     ++count;
 17 }

[C# source code]
  1 using System;
  2 using System.Text;
  3 using System.Runtime.InteropServices;
  4 namespace client {
  5     class MainClass {
  6         const string LIBRARY_NAME = "libmoon.so";
  7         [DllImport(LIBRARY_NAME)] 
  8             private static extern void getsomething(StringBuilder str, int size);
  9         public static void Main (string[] args) {
 10             StringBuilder buf1 = new StringBuilder(64);
 11             for( int i = 0; i < 2; ++i ){
 12                 getsomething(buf1, buf1.Capacity);
 13                 Console.WriteLine("pattern1 " + buf1.ToString());
 14             }   
 15             StringBuilder buf2 = new StringBuilder(64);
 16             for( int i = 0; i < 2; ++i ){
 17                 buf2.Length = 0; // <-this
 18                 getsomething(buf2, buf2.Capacity);
 19                 Console.WriteLine("pattern2 " + buf2.ToString());
 20             }   
 21         }   
 22     }   
 23 }   

[Output]
pattern1 sun
pattern1 sun -> not inteded result
pattern2 sun
pattern2 moon -> intended result
Comment 1 Zoltan Varga 2012-10-25 22:15:33 UTC
Fixed in master/2.10. Thanks for the report.