On Aug 9, 2008, at 5:24 PM, Dennis Harms wrote:

I've created a class with some member variables of type NSString*. In the init function of the class, I write something into those strings. Now I call a function of the initialized class instance as a new thread and try to read from those member variables. This leads to a crash... Can someone give me a
hint as to why this won't work?

Here's my code, shrunk down to just the problematic part:

---------- MyClass.h -----------------------------
@interface MyClass: NSObject
{
@private
    NSString* baseURL;
}

- (id) initWithHost: (NSString*) _url;
- (void) doSomething: (id) object;

@end
----------------------------------------------------------

---------- MyClass.m -----------------------------
@implementation MyClass

- (id) initWithHost: (NSString*) _url
{
    self = [super init];
    baseURL = [NSString stringWithString: _url];

One thing I notice right off is that you're initializing an instance variable using a method that creates an autoreleased string, which will go away next time through the application's event loop. How about "baseURL = [_url retain];" instead? If the string pointed to by _url is really immutable, you just need to increment the retain count so it'll continue to exist for the lifetime of your object (and then be sure to call [_url release] in your object's dealloc method so you don't leak memory).

    return self;
}

- (void) doSomething: (id) object
{
NSString *url = [NSString stringWithString: baseURL]; // <-- the crash
occurs even when just reading the member

You might check that baseURL is still valid just before executing this statement when you have your app set up to execute this in other than the main thread. Making the change, above, will likely fix the crash.

Also, why are you calling -stringWithString: here? You already have a perfectly good string in baseURL. Use it directly.

}

@end
----------------------------------------------------------

I've tried calling the function in the same thread, as well as three
different ways of calling it in a seperate thread. Every time there's
another thread involved, it crashes when accessing the member variable. If
the function doesn't access any members, the code works just fine...

instance = [[MyClass alloc] initWithHost: txtHost.text];
[instance doSomething: nil];   // Works
[instance performSelectorInBackground: @selector(doSomething:) withObject:
nil];   // Doesn't work
[[[NSThread alloc] initWithTarget: instance selector:
@selector(doSomething:) object: nil] start];   // Doesn't work
[NSThread detachNewThreadSelector: @selector(doSomething:) toTarget:
instance withObject: nil];   // Doesn't work

_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to