Re: AppKit Addition usage
Re: AppKit Addition usage
- Subject: Re: AppKit Addition usage
- From: "James Trankelson" <email@hidden>
- Date: Thu, 23 Aug 2007 20:28:28 -0400
The code leading up to this point is a bit convoluted. There are other
threads involved with the process, so maybe there's something there.
To start, here's what's going on:
I'm managing a second, POSIX thread in addition to my main app thread.
When an event happens in this secondary thread (an error condition), I
want to send it up to the main app. So, I do it by sending a
notification like so:
[[NSNotificationCenter defaultCenter]
postNotificationName:@"handleError" object:[NSString
stringWithString:evalErrorString]];
Now, the handling of this notification is in the wrong thread, so
there is this funny bubbling up mechanism to send the notification to
the main thread, something like:
(found in the apple docs)
if( [NSThread currentThread] != notificationThread ) {
// Forward the notification to the correct thread
[notificationLock lock];
[notifications addObject:notification];
[notificationLock unlock];
[notificationPort sendBeforeDate:[NSDate date] components:nil
from:nil reserved:0];
}
My main thread eventually gets the notification, and when I do, I try
to use the NSAttributedString Addition "size". This crashes. To make
sure this is running in the appropriate thread, I'm printing the
threads out like so
(in awakeFromNib):
NSLog(@"Thread main %@", [NSThread currentThread]);
(and before the "size" call):
NSLog(@"Thread before size %@", [NSThread currentThread]);
They have the same address.
-jt
On 8/23/07, Andrew Merenbach <email@hidden> wrote:
> Hi, James,
>
> That is indeed odd. Is there anything of any sort going on in a
> secondary thread prior to the crash? Sharing your code with the list
> might help, if you're able.
>
> Cheers,
> Andrew
>
> On Aug 23, 2007, at 5:09 PM, James Trankelson wrote:
>
> > Hmm. Thanks.
> >
> > Oddly enough, I am making this call in the main thread, and it still
> > crashes. Odd...
> >
> > -jt
> >
> > On 8/23/07, Andrew Merenbach <email@hidden> wrote:
> >> Hi, James,
> >>
> >> To quote an article on the Cocoa-Dev web site (not to be confused
> >> with the mailing list):
> >>> Note: AppKit is not thread-safe, so this code might do harm to your
> >>> computer, make you unpopular and/or make your wife/husband
> >>> pregnant. Use at your own risk (and I'd not recommend to). Look at
> >>> distributed objects if you want to...
> >>>
> >> You can't use the -size method from any thread but the main one.
> >> Perhaps try something like this:
> >>
> >> - (void)myNewThreadMethod {
> >> NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
> >>
> >> NSAttributedString *string = [[NSAttributedString alloc]
> >> initWithString:@"hello!"];
> >> [self performSelectorOnMainThread:@selector
> >> (measureSizeOfAttributedString:) withObject:string
> >> waitUntilDone:YES];
> >> [string release];
> >>
> >> // now, you can do something with the accessor
> >> NSSize theSize = [self attributedstringSize];
> >>
> >> [pool release];
> >> }
> >>
> >> - (void)measureSizeOfAttributedString:(NSAttributedString *)aString {
> >> [self setAttributedStringSize:[aString size]];
> >> }
> >>
> >> - (NSSize)attributedStringSize {
> >> @synchronized(self) {
> >> return attributedStringSize;
> >> }
> >>
> >> return NSZeroSize;
> >> }
> >>
> >> - (void)setAttributedStringSize:(NSSize)aSize {
> >> @synchronized(self) {
> >> attributedStringSize = aSize;
> >> }
> >> }
> >>
> >>
> >> On Aug 23, 2007, at 2:03 PM, James Trankelson wrote:
> >>
> >>> Hi,
> >>>
> >>> I've transferred over some code that uses NSAttributedString AppKit
> >>> extensions, and it crashes in my new environment with a BAD_ACCESS.
> >>> I'm trying to call the size method of the NSAttributedString
> >>> extension. Is there something I've forgotten to do to make this
> >>> work?
> >>> Shouldn't I just be able to include the AppKit framework and have it
> >>> work ok?
> >>>
> >>> These calls don't need to be in any particular thread or anything,
> >>> right?
> >>>
> >>> Thanks
> >>>
> >>> -jt
> >>> _______________________________________________
> >>>
> >>> 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:
> >>> 40ucla.edu
> >>>
> >>> This email sent to email@hidden
> >>
> >> _______________________________________________
> >>
> >> 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:
> >> 40gmail.com
> >>
> >> This email sent to email@hidden
> >>
>
>
_______________________________________________
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