Re: Attributed Strings in Distributed Objects
Re: Attributed Strings in Distributed Objects
- Subject: Re: Attributed Strings in Distributed Objects
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Wed, 5 Sep 2007 15:34:58 +0200
On 5 Sep 2007, at 13:05, Alastair Houghton wrote:
On 5 Sep 2007, at 08:23, Gerriet M. Denkmann wrote:
I have an object, which responds to this message:
- (NSAttributedString *)dummyWithStyle: (BOOL)withStyle ;
{
NSString *s = withStyle ? @"with Style" : @"without Style";
NSMutableAttributedString *a = [ [ [ NSMutableAttributedString
alloc ] initWithString: s ] autorelease ];
if ( withStyle )
{
NSParagraphStyle *p = [ NSParagraphStyle defaultParagraphStyle ];
unsigned int length = [ a length];
NSRange all = NSMakeRange( 0, length );
[ a addAttribute: NSParagraphStyleAttributeName value: p
range: all ];
};
NSLog(@"%s will return %@", __FUNCTION__, a );
return a;
}
If the flag "withStyle" is NO, or if sender and reveiver live in
the same thread (i.e. [myObject isProxy ] = 0) then all is fine.
But when I do: [proxyForMyObject dummyWithStyle: YES ] (with
[proxyForMyObject isProxy] = 1) then the program will hang after
writing the log message immediately before the return.
I think (though I could be wrong) that some object is being proxied
in the main thread (possibly the attributed string itself, though I
would have thought that these were always copied... it could be
something to do with the attribute structure, or maybe your
NSParagraphStyle that's causing the problem).
The main thread has a method doButton: which contains:
NSLog(@"%s will do: [ %s dummyWithStyle: %s ] in main thread %p",
__FUNCTION__, [o isProxy] ? "proxy" : "real", style ? "YES" : "NO",
[NSThread currentThread]);
NSAttributedString *a = [o dummyWithStyle: style] ;
NSLog(@"%s got %s AttributedString = \"%@\"", __FUNCTION__, [a
isProxy] ? "proxy" : "real", [a string]);
This is the output:
... -[SWDocument doButton:] will do: [ proxy dummyWithStyle: NO ] in
main thread 0x306cf0
... -[SXLeaf dummyWithStyle:] will return "without Style" in thread
0x3a0300
... -[SWDocument doButton:] got real AttributedString = "without Style"
no problem so far. But now with NSParagraphStyle added:
... -[SWDocument doButton:] will do: [ proxy dummyWithStyle: YES ] in
main thread 0x306cf0
... -[SXLeaf dummyWithStyle:] will return "with Style" in thread
0x3a0300
Now the app is not crashing at all, it just hangs.
When I run it under the debugger and hit the "Pause" button, then I
will see the stack as quoted.
Anyway, as a result, your thread needs to run a run loop in order
to process messages from the main thread. I'm guessing that your
thread has terminated or is doing something other than running a
run loop.
Well, the main thread obviously has a run loop; and the other thread
also has one. I followed the example in "Communicating With
Distributed Objects" in "Multithreading Programming Topics".
One possible way around this would be to subclass e.g.
NSParagraphStyle (or whatever is causing the problem) and override -
replacementObjectForPortCoder: to return self (assuming that it
doesn't already). As I say, I think NSAttributedStrings are copied
rather than proxied, and your backtrace seems to support that, so I
don't think it's the string itself that's the issue (and if it was,
you could just use "bycopy" in a declaration somewhere).
Well, maybe it's NSParagraphStyle, maybe it's any kind of text
attachment, maybe any deep structure (what about an NSArray
containing objects, which again contain arrays, which again... - how
deep a copy is NSConnection prepared to make?) - I am just too tired
to find out.
I just got some other idea and made myself a new method:
- (NSData *)archivedDummyWithStyle: (BOOL)withStyle ;
{
// ... same as before in dummyWithStyle:
// followed by:
NSData *c = [NSArchiver archivedDataWithRootObject: a];
return c;
}
And now I can get my attributed strings with or without paragraph
styles. Might take a few extra milliseconds, but I don't care.
Or, you could run a run loop in your other thread.
As mentioned, the worker thread does have a run loop.
Kind regards,
Gerriet.
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden