# Using NSAllocateMemoryPages in threads.



## Untitled (Jul 8, 2004)

I have a program that launches a thread which includes the following (dumbed down) 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 (Jul 8, 2004)

Why aren't you using malloc()?


----------



## Untitled (Jul 8, 2004)

anarchie said:
			
		

> Why aren't you using malloc()?


Hmm... probably because the simplest solution is always overlooked.   

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:

```
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!


----------



## Darkshadow (Jul 8, 2004)

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).


----------



## Untitled (Jul 8, 2004)

Darkshadow said:
			
		

> NSString *aString=[[NSString alloc]initWithUTF8String:buffer];
> 
> NSLog(@"%@",aString);
> 
> ...


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 (Jul 8, 2004)

NSLog(@"%s", buffer)

Remember your printf codes - most of them work with NSLog.


----------



## Untitled (Jul 8, 2004)

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.


----------



## anarchie (Jul 8, 2004)

Oh, right.  For UTF-16 strings the sequence is %S

Format Strings


----------



## Untitled (Jul 8, 2004)

I am using the [NSString getCharacters:buffer range:range] and want to allocate it in a new string with [NSString stringWithFormat"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.


----------



## anarchie (Jul 8, 2004)

Screw performance reasons.  The slowness is NOT where you think it is.

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


----------

