More on: converting C-strings to NSString
More on: converting C-strings to NSString
- Subject: More on: converting C-strings to NSString
- From: email@hidden
- Date: Tue, 23 Oct 2001 15:10:02 -0400
On Monday, October 22, 2001, at 11:03 PM, mikevannorsdel wrote:
>
This would work much better:
>
>
NSMutableString * secondbuffer;
>
char buffer[2];
>
>
...
>
secondbuffer=[[NSMutableString alloc] init];
>
>
(in a loop)
>
[secondbuffer appendString:[NSString stringWithCString:scratch]];
>
>
>
Make sure and release secondbuffer before allocating it again.
>
>
As for the errors, are you doing this in a secondary thread? Remember
>
that you need to create an autorelease pool for that thread before
>
using autoreleased objects.
>
Also, you can't do [results setStringValue:secondbuffer] anywhere
>
other than the main thread.
But I need to. I can not have my interface lock up until the rest of the
program completes.
What I am doing, is the following:
1. creating a new thread
2. the new thread calls popen() (which, in essence forks(), exec()-s and
pipes output to a FILE pointer).
3. the new thread waits for the process to finish and then displays the
output in a NSTextView object
4. the new thread exits
Nick
>
>
On Monday, October 22, 2001, at 08:26 PM, email@hidden wrote:
>
>
> Hello.
>
>
>
> I have been working on this bug for a while, and I can't seem to figure
>
> out what the problem is. I am first converting a C-string into NSString
>
> using the following syntax:
>
>
>
> NSString * scratch;
>
> NSString * secondbuffer;
>
> char buffer[2];
>
>
>
> ...
>
> [secondbuffer init];
>
>
>
> (in a loop)
>
> [scratch initWithCString: buffer];
>
> secondbuffer = [secondbuffer stringByAppendingString: scratch];
>
>
>
> This is probably not the best way of doing it, however, it runs fine,
>
> until I do:
>
>
>
> NSTextView * results;
>
>
>
> ...
>
>
>
> [results setString:secondbuffer];
>
>
>
> Here it screws up, giving me the following error output:
>
>
>
> 2001-10-22 22:17:19.153 Nmap[2219] *** _NSAutoreleaseNoPool(): Object
>
> 0xcfd620 of class NSCFString autoreleased with no pool in place - just
>
> leaking
>
> 2001-10-22 22:17:19.154 Nmap[2219] *** _NSAutoreleaseNoPool(): Object
>
> 0x14bc190 of class NSCFString autoreleased with no pool in place - just
>
> leaking
>
> 2001-10-22 22:17:19.154 Nmap[2219] *** _NSAutoreleaseNoPool(): Object
>
> 0x14e0be0 of class NSException autoreleased with no pool in place -
>
> just
>
> leaking
>
> 2001-10-22 22:17:19.154 Nmap[2219] An uncaught exception was raised
>
> 2001-10-22 22:17:19.154 Nmap[2219] *** -[NSTextView
>
> replaceCharactersInRange:withString:]: nil NSString given.
>
> 2001-10-22 22:17:19.155 Nmap[2219] *** Uncaught exception:
>
> <NSInvalidArgumentException> *** -[NSTextView
>
> replaceCharactersInRange:withString:]: nil NSString given.
>
>
>
> As far as I can figure it, the secondbuffer string gets released? I
>
> have
>
> no idea why. It is declared globally, i.e. outside any classes or
>
> functions.
>
>
>
> Please help, I am unsure what to do with this. I have been struggling
>
> two whole days with this...
>
>
>
> Nick
>
>
Nick Hristov
Slayter Box 1194
Denison University
Granville, OH 43023