• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: UInt8 > NSString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: UInt8 > NSString


  • Subject: Re: UInt8 > NSString
  • From: arri <email@hidden>
  • Date: Tue, 6 Mar 2007 16:01:21 +0100

On Mar 6, 2007, at 05:18 27, Bob Smith wrote:
..Hmmm, maybe, I would have to see your code to be sure.
Briefly, here is the thread-safe approach:
- (NSString *)makeTheString
{
  char theBytes[10];

  (do something to fill in theBytes)

  NSString *theString = [NSString stringWithCString:theBytes];
  return(theString);
}


Safe, because theBytes[] is dynamically allocated each time the method is called; which is why you were having your original problem, but now it's a good thing because it means each thread will have it's own memory.


ok, then this should indeed be threadsafe, since 'MACAddress' is being allocated in the -mainMacAddress method.
thanks everyone for your input!
gr
arri






static kern_return_t FindEthernetInterfaces(io_iterator_t *matchingServices)
{
kern_return_t kernResult;
CFMutableDictionaryRef matchingDict;
CFMutableDictionaryRef propertyMatchDict;
matchingDict = IOServiceMatching(kIOEthernetInterfaceClass);
if (NULL == matchingDict) {
printf("IOServiceMatching returned a NULL dictionary.\n");
} else {
propertyMatchDict = CFDictionaryCreateMutable(kCFAllocatorDefault, 0,
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);


if (NULL == propertyMatchDict) {
printf("CFDictionaryCreateMutable returned a NULL dictionary.\n");
} else {
CFDictionarySetValue(propertyMatchDict, CFSTR (kIOPrimaryInterface), kCFBooleanTrue);
CFDictionarySetValue(matchingDict, CFSTR(kIOPropertyMatchKey), propertyMatchDict);
CFRelease(propertyMatchDict);
}
}
kernResult = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, matchingServices);
if (KERN_SUCCESS != kernResult) {
printf("IOServiceGetMatchingServices returned 0xx\n", kernResult);
}
return kernResult;
}


static kern_return_t GetMACAddress(io_iterator_t intfIterator, UInt8 *MACAddress, UInt8 bufferSize)
{
io_object_t intfService;
io_object_t controllerService;
kern_return_t kernResult = KERN_FAILURE;
if (bufferSize < kIOEthernetAddressSize) {
return kernResult;
}
bzero(MACAddress, bufferSize);
while (intfService = IOIteratorNext(intfIterator)) {
CFTypeRef MACAddressAsCFData;
kernResult = IORegistryEntryGetParentEntry(intfService,
kIOServicePlane,
&controllerService);


if (KERN_SUCCESS != kernResult) {
printf("IORegistryEntryGetParentEntry returned 0xx\n", kernResult);
}
else {
MACAddressAsCFData = IORegistryEntryCreateCFProperty (controllerService,
CFSTR(kIOMACAddress),
kCFAllocatorDefault,
0);
if (MACAddressAsCFData) {CFDataGetBytes(MACAddressAsCFData, CFRangeMake(0, kIOEthernetAddressSize), MACAddress);
CFRelease(MACAddressAsCFData);
}
(void) IOObjectRelease(controllerService);
}
(void) IOObjectRelease(intfService);
}
return kernResult;
}





@implementation TheClass

.
.
.

- (NSString*)mainMacAddress {
kern_return_t kernResult = KERN_SUCCESS;
io_iterator_t intfIterator;
UInt8 MACAddress[kIOEthernetAddressSize];
NSString *addressString = nil;
kernResult = FindEthernetInterfaces(&intfIterator);

if (KERN_SUCCESS != kernResult) {
addressString =[NSString stringWithString:@"000000000000"];
} else {
kernResult = GetMACAddress(intfIterator, MACAddress, sizeof (MACAddress));
if (KERN_SUCCESS != kernResult) {
addressString =[NSString stringWithString:@"000000000000"];
} else {
addressString =[NSString stringWithFormat:@"xxxxx% 02x",
MACAddress[0], MACAddress[1], MACAddress[2],MACAddress[3], MACAddress[4], MACAddress[5]];
}
}
(void) IOObjectRelease(intfIterator); // Release the iterator.
return addressString;
}


.
.
.

@end



_______________________________________________

Cocoa-dev mailing list (email@hidden)

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:
This email sent to email@hidden


References: 
 >shortcut for selecting in NSPopUpButton (From: email@hidden)
 >UInt8 > NSString (From: arri <email@hidden>)
 >Re: UInt8 > NSString (From: Bob Smith <email@hidden>)
 >Re: UInt8 > NSString (From: arri <email@hidden>)
 >Re: UInt8 > NSString (From: Bob Smith <email@hidden>)

  • Prev by Date: Re: Change password on DMG
  • Next by Date: MEETING: Chicago CocoaHeads / CAWUG Tuesday 3/13 - Core Image
  • Previous by thread: Re: UInt8 > NSString
  • Next by thread: Cocoa-dev Digest, Vol 4, Issue 203
  • Index(es):
    • Date
    • Thread