Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Thread problem

Thread problem
Thread Tools
Dedicated MacNNer
Join Date: Sep 2000
Status: Offline
Reply With Quote
Apr 9, 2002, 04:58 PM
 
Hi!

I found the code att the bottom of this post somewhere in this forum. I made an interface with a textField, a textView and a button. It's an example on how to use threads.

When I write in the textField and press a OK button my text should appear in the textView 60 times without locking up the interface.

But all it does is crash all the time with this message:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<font color = blue>2002</font>-<font color = blue>04</font>-<font color = blue>10</font> <font color = blue>00</font>:<font color = blue>07</font>:<font color = blue>29.925</font> ThreadTest[<font color = blue>1872</font>] *** Assertion failure in -[NSMutableRLEArray objectAtIndex:effectiveRange:], String.subproj/NSAttributedString.m:<font color = blue>923</font>
<font color = blue>2002</font>-<font color = blue>04</font>-<font color = blue>10</font> <font color = blue>00</font>:<font color = blue>07</font>:<font color = blue>29.925</font> ThreadTest[<font color = blue>1872</font>] An uncaught exception was raised
<font color = blue>2002</font>-<font color = blue>04</font>-<font color = blue>10</font> <font color = blue>00</font>:<font color = blue>07</font>:<font color = blue>29.926</font> ThreadTest[<font color = blue>1872</font>] Access invalid attribute location <font color = blue>0</font> (length <font color = blue>0</font>)
<font color = blue>2002</font>-<font color = blue>04</font>-<font color = blue>10</font> <font color = blue>00</font>:<font color = blue>07</font>:<font color = blue>29.926</font> ThreadTest[<font color = blue>1872</font>] *** Uncaught exception: &lt;NSInternalInconsistencyException&gt ; Access invalid attribute location <font color = blue>0</font> (length <font color = blue>0</font>)
</font>[/code]

I'm quite a newbie to this Cocoa thing, so any help would be nice...

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#import <font color = red>"myClass.h"</font>

@implementation myClass

- (IBAction)OKBUTTON id)sender;
{
[NSApplication detachDrawingThread: @selector (doSomething toTarget:self withObject:[[input stringValue] retain]];
}


- (void)doSomething NSString *)inputString
{
NSAutoreleasePool* pool;
NSRange range;
int length, i;

range.location = <font color = blue>0</font>;
range.length = [inputString length];

for(i = <font color = blue>0</font>;i &lt; <font color = blue>60</font>; i++)
{

pool = [[NSAutoreleasePool alloc] init];


[output replaceCharactersInRange:range withString:inputString];
range.location += length;
[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:<font color = blue>1</font>]];

[pool release];

}
[inputString release];
}

@end
</font>[/code]

Here's myClass.h:
<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#import &lt;Cocoa/Cocoa.h&gt;

@interface myClass : NSView
{
IBOutlet id input;
IBOutlet id output;
}
- (IBAction)OKBUTTON id)sender;
- (void)doSomething NSString *)inputString;

@end
</font>[/code]

[ 04-09-2002: Message edited by: thanatos ]
     
Dedicated MacNNer
Join Date: Jun 2000
Location: Dundas, Ontario, Canada
Status: Offline
Reply With Quote
Apr 10, 2002, 02:42 PM
 
Well, I would wonder about the call to "replaceCharactersInRange:withString:" because I am not sure if it likes being told to replace a part of a string that does not exist (I am not sure, though, since I haven't used that in a while).

Also, this isn't the best application of multi-threading since it requires a thread other than the main thread to modify the display. It _should_ be fine but I have had kernel panics while doing that before (although it was an NSOpenGLView and I was explicitly calling "display" after making my modifications to data). Unless you are using this to experiment with threads it would be better to us an NSTimer.

Hope that helps,
Jeff.
Spectral Class
"Shedding Light on Innovation"
     
Senior User
Join Date: Mar 2000
Location: Ithaca, NY
Status: Offline
Reply With Quote
Apr 10, 2002, 03:01 PM
 
Your problem is that you are replacing characters in an invalid range. I assume that when you start out, your output view is empty, yes? Well, then when you try to replace the characters in the range {0, [inputString length]}, this range goes beyond the range of the characters in the text view, since there are zero characters. What you want to do is to set the range.length to 0 instead of [inputString length] initially. That way, you start out replacing the range {0, 0} and then increment the location each time so that the range is at the end of the text view.

One additional thing, since you are doing these changes from another thread, you might want to call [output display] after you insert the new characters each time. The view will not be automatically redrawn right away when you insert the new characters, but will instead be redrawn by the main thread the next time it goes through the event loop. This may result in the main thread trying to update at the same time you are inserting new characters, which could definitely lead to Bad Things™.
     
Dedicated MacNNer
Join Date: Sep 2000
Status: Offline
Reply With Quote
Apr 11, 2002, 04:51 PM
 
Originally posted by bewebste:
<STRONG>Your problem is that you are replacing characters in an invalid range. I assume that when you start out, your output view is empty, yes? Well, then when you try to replace the characters in the range {0, [inputString length]}, this range goes beyond the range of the characters in the text view, since there are zero characters. What you want to do is to set the range.length to 0 instead of [inputString length] initially. That way, you start out replacing the range {0, 0} and then increment the location each time so that the range is at the end of the text view.

One additional thing, since you are doing these changes from another thread, you might want to call [output display] after you insert the new characters each time. The view will not be automatically redrawn right away when you insert the new characters, but will instead be redrawn by the main thread the next time it goes through the event loop. This may result in the main thread trying to update at the same time you are inserting new characters, which could definitely lead to Bad Things™.</STRONG>
I put some text in the textbox and changed the range.length to 0. It now works a bit better, it still crashes, but after a second. Before it crashed right away.

this is the error I get now:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
<font color = blue>2002</font>-<font color = blue>04</font>-<font color = blue>11</font> <font color = blue>23</font>:<font color = blue>39</font>:<font color = blue>27.605</font> ThreadTest[<font color = blue>2813</font>] *** Assertion failure in -[NSMutableRLEArray objectAtIndex:effectiveRange:], String.subproj/NSAttributedString.m:<font color = blue>923</font>
<font color = blue>2002</font>-<font color = blue>04</font>-<font color = blue>11</font> <font color = blue>23</font>:<font color = blue>39</font>:<font color = blue>27.606</font> ThreadTest[<font color = blue>2813</font>] An uncaught exception was raised
<font color = blue>2002</font>-<font color = blue>04</font>-<font color = blue>11</font> <font color = blue>23</font>:<font color = blue>39</font>:<font color = blue>27.606</font> ThreadTest[<font color = blue>2813</font>] Access invalid attribute location <font color = blue>-2021289070</font> (length <font color = blue>19</font>)
<font color = blue>2002</font>-<font color = blue>04</font>-<font color = blue>11</font> <font color = blue>23</font>:<font color = blue>39</font>:<font color = blue>27.606</font> ThreadTest[<font color = blue>2813</font>] *** Uncaught exception: &lt;NSInternalInconsistencyException&gt ; Access invalid attribute location <font color = blue>-2021289070</font> (length <font color = blue>19</font>)
</font>[/code]

Maybe I have done something wrong, but my code looks like this now:

<BLOCKQUOTE><font size="1"face="Geneva, Verdana, Arial">code:</font><HR><pre><font size=1 face=courier>
#import <font color = red>"myClass.h"</font>

@implementation myClass

- (IBAction)OKBUTTON id)sender;
{
[NSApplication detachDrawingThread: @selector (doSomething toTarget:self withObject:[[input stringValue] retain]];
}


- (void)doSomething NSString *)inputString
{
NSAutoreleasePool* pool;
NSRange range;
int length, i;

range.location = <font color = blue>0</font>;
range.length = <font color = blue>0</font>;

for(i = <font color = blue>0</font>;i &lt; <font color = blue>60</font>; i++)
{
pool = [[NSAutoreleasePool alloc] init];

[output replaceCharactersInRange:range withString:inputString];
[output display];
range.location += length;

[NSThread sleepUntilDate:[NSDate dateWithTimeIntervalSinceNow:<font color = blue>1</font>]];
[pool release];

}
[inputString release];
}

@end

</font>[/code]
     
   
Thread Tools
Forum Links
Forum Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Top
Privacy Policy
All times are GMT -5. The time now is 09:43 AM.
All contents of these forums © 1995-2011 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.7 © 2000-2011, Jelsoft Enterprises Ltd., Content Relevant URLs by vBSEO 3.3.2