Using NSAllocateMemoryPages in threads.

Untitled

Untitled
I have a program that launches a thread which includes the following (dumbed down) code:
Code:
unichar *buffer;	
buffer = NSAllocateMemoryPages(50 * sizeof(unichar));
NSDeallocateMemoryPages(buffer, 50);
unfortunately this causes a crash with the following text in the console:
*** NSAllocateMemoryPages(-24631) failed
*** Uncaught exception: <NSInvalidArgumentException> *** NSAllocateMemoryPages(-24631) failed

I have already learned (and implemented) that you need a new autorelease pool in the thread and I assume something similar to this needs to be done to fix this problem. If I place this code in the main application thread, it works fine. Any suggestions?
 
anarchie said:
Why aren't you using malloc()?
Hmm... probably because the simplest solution is always overlooked. :D

Actually, I was looking at how GNUStep worked their functions in the NSString classes and figured that would be the best way.

Just for future reference to whomever looks at this forum, the below code works fine:
Code:
buffer = malloc((buffLength + 1) * sizeof(unichar));
buffer[buffLength] = (unichar)0; // I just add this for safety
free(buffer);
As a follow up question, how might I go about printing this sexy string with NSLog?

Thanks!
 
NSString *aString=[[NSString alloc]initWithUTF8String:buffer];

NSLog(@"%@",aString);

[aString release];

...of course, that said, I haven't actually used GNUStep, so I can't say for sure that NSString has the -initWithUTF8String method on it. If it doesn't, you can probably use -initWithCString, but that will most likely lose any unicode encodings (usually, c strings use ASCII, so the -initWithCString method usually loses anything other than what's in the ASCII character set).
 
Darkshadow said:
NSString *aString=[[NSString alloc]initWithUTF8String:buffer];

NSLog(@"%@",aString);

[aString release];

...of course, that said, I haven't actually used GNUStep, so I can't say for sure that NSString has the -initWithUTF8String method on it. If it doesn't, you can probably use -initWithCString, but that will most likely lose any unicode encodings (usually, c strings use ASCII, so the -initWithCString method usually loses anything other than what's in the ASCII character set).
I am using cocoa and and using this method currently.
I was hoping that I could just output the buffer without putting it in an NSString first however. Is it possible to do that with one of those magic %letter statements?
 
anarchie said:
NSLog(@"%s", buffer)

Remember your printf codes - most of them work with NSLog.
I tried that and it did not work. :/ Using NSString to print it is working however so I am not sure if that is a sign that there is not a memory problem.
 
I am using the [NSString getCharacters:buffer range:range] and want to allocate it in a new string with [NSString stringWithFormat:mad:"this is a buffer:%s", buffer]

I know I could just add a few more lines however I would like to implement it in this fashion if at all possible for performance reasons and so I can understand how to do it.
 
Screw performance reasons. The slowness is NOT where you think it is.

[NSString stringWithFormat:mad:"this is another string: %@", anotherString]
 
Back
Top