[Mono-osx] Registering an NSThread with Mono

Kevin Heeney koheeney at gmail.com
Sun Jan 25 19:44:52 EST 2009


So this is sort of a thread bump I guess.  I am still having the below
issue; however I am now attaching more information I hope.  I have ObjC
classes that I have written that invoke my Mono classes.  These are working
and I have written an app utilizing these. I am now experimenting using a
library called MacFuse, in essence I pass it an ObjC object which it holds
and calls (a delegate object).  When that object is called from MacFuse
everything works except for when invoking mono, my variables come back null.
The below function is the function that errors out.  My ObjC class still has
all the data it had and when calling this same object from my main thread it
works, so it can't be garbage collection for either ObjC or Mono because I
can still access all of the data from my main thread.  The error is only
when being called from another API (and in this case I believe the OSX
kernel).

The below function outputs the following when being called from the MacFuse
thread:
2009-01-25 18:28:12.885 InvokeMonoTest[670] val is null in
getStringFieldValue
2009-01-25 18:28:12.885 InvokeMonoTest[670] p is null in getStringFieldValue
2009-01-25 18:28:12.885 InvokeMonoTest[670]  ERROR in attributesOfItemAtPath
  *** -[NSPlaceholderString initWithUTF8String:]: NULL cString

-(NSString*) getStringFieldValue: (NSString*) FieldName

{


 MonoClassField *field;

MonoString* val;


mono_thread_attach(domain);



field = mono_class_get_field_from_name ([MonoGlobals monoFClass], [FieldName
UTF8String]);


 if (!field) {

fprintf (stderr, "Can't find field %s in MyType\n",[FieldName UTF8String]);

exit (1);

}


 if (!fsObject) NSLog(@"fsObject is null in getStringFieldValue");


 mono_field_get_value (fsObject, field, &val);


 if (!val) NSLog(@"val is null in getStringFieldValue");


 char *p;

p = mono_string_to_utf8 (val);

 if (!p) NSLog(@"p is null in getStringFieldValue");


 NSString* retStr = [[NSString alloc] initWithUTF8String:p];

return retStr;


}






On Wed, Nov 26, 2008 at 7:20 PM, Kevin Heeney <koheeney at gmail.com> wrote:

> Okay, so I think it is an issue somehow with how I am invoking Mono.  When
> being called from the main thread, the following code works.  When being
> called from a separate thread, on the same object, mono_string_to_utf8
> (val) below returns NULL but does not throw an exception.
>
>
> mono_thread_attach(domain);
>
> MonoClassField *field;
> MonoString* val;
>
> field = mono_class_get_field_from_name (myMonoclass, [FieldName
> UTF8String]);
>
> mono_field_get_value (myMonoObject, field, &val);
> char *p;
> p = mono_string_to_utf8 (val);  //p here will be null when being called
> from a secondary thread.
>
> NSString* retStr = [[NSString alloc] initWithUTF8String:p]; //on secondary
> threads, this line throws the exception "-[NSPlaceholderString
> initWithUTF8String:]: NULL cString" because p is null
> return retStr;
>
> I am doing more checks (like checking to make sure the field is found, but
> I have removed that code in this sample).
>
> Any suggestions?
>
> Thanks,
> Kevin
>
>
>
> On Nov 26, 2008, at 1:15 PM, Kevin Heeney wrote:
>
> I was thinking I had to pass mono_thread_attach a reference to a thread.
>  Would calling mono_thread_attach(myDomain); work?   If so, I might have
> a different issue.
> Thanks,
> Kevin
>
> On Nov 26, 2008, at 1:03 PM, Kevin Heeney wrote:
>
> Is there any way to register an NSThread with Mono?  I am using a 3rd party
> framework which I pass a delegate class and it launches a new Thread.  I
> have that class getting a reference to this created Thread using [NSThread
> currentThread].  I now need to call Mono from this thread but I need
> to register the thread using "mono_thread_attach" I believe.  Is there
> anyway that I can do this with an NSThread?
> Thanks,
> Kevin
>
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-osx/attachments/20090125/d64ebb0d/attachment.html 


More information about the Mono-osx mailing list