Having trouble getting objects to use a specific zone.

The following test code snippet demonstrates the problem I'm having:

        NSZone * zone = NSCreateZone(vm_page_size * 2, vm_page_size, YES);
        if (zone)
        {
                NSSetZoneName(zone, @"IN THE ZONE!!!");

                char * cstring = (char *)NSZoneCalloc(zone, 50, 1);
                if (cstring)
                {
                        sprintf(cstring, "abc!!");
                        NSLog(@"   zone = %p", (void *)zone);
                        NSLog(@"cstring = %p", (void *)cstring);
NSLog(@" end = %p", (void *)(((unsigned char *)cstring)+(vm_page_size * 2)));
                        
                        NSZone * _czone = NSZoneFromPointer((void *)cstring);
                        if (_czone)
                        {
                                NSLog(@"cstring.zone name = %@", 
NSZoneName(_czone));
                        }
                        
                }
                
NSString * string = [[NSString allocWithZone:zone] initWithFormat:@"abc!!!"];
                if (string)
                {
                        NSLog(@"  zone = %p", (void *)zone);
                        NSLog(@"string = %p", (void *)string);
NSLog(@" end = %p", (void *)(((unsigned char *)string)+(vm_page_size * 2)));
                        
                        NSZone * _zone = NSZoneFromPointer((void *)string);
                        if (_zone)
                        {
                                NSLog(@"string.zone name = %@", 
NSZoneName(_zone));
                        }
                }
        }


... this code generates the following output:

2010-08-09 22:06:22.765 zonetest[24440:a0f]    zone = 0x114658000
2010-08-09 22:06:22.768 zonetest[24440:a0f] cstring = 0x114b00010
2010-08-09 22:06:22.769 zonetest[24440:a0f]     end = 0x114b02010
2010-08-09 22:06:22.770 zonetest[24440:a0f] cstring.zone name = IN THE ZONE!!!
2010-08-09 22:06:22.770 zonetest[24440:a0f]   zone = 0x114658000
2010-08-09 22:06:22.771 zonetest[24440:a0f] string = 0x10010ab30
2010-08-09 22:06:22.771 zonetest[24440:a0f]    end = 0x10010cb30
2010-08-09 22:06:22.772 zonetest[24440:a0f] string.zone name = DefaultMallocZone


As you can see, the c string is successfully allocated from space in the custom zone. But the NSString object is allocated from the default zone, despite having called +[NSObject allocWithZone:].

Anyone know the trick to this?

Thanks.

Chuck






_______________________________________________

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 arch...@mail-archive.com

Reply via email to