Replacing default CFAllocator breaks application?
Replacing default CFAllocator breaks application?
- Subject: Replacing default CFAllocator breaks application?
- From: Jim Correia <email@hidden>
- Date: Wed, 29 Mar 2006 13:34:31 -0500
I'd like to replace the default CFAllocator in a debug build of my
application so I can keep some allocation statistics and hopefully
track down a memory related bug. However, when I do so, the
application misbehaves. If I do this to TextEdit, lots of text goes
"missing".
the edit window titlebar
text in the text view
lots of control titles in the preferences window
My debug allocator currently wraps the previous default allocator,
but I've mapped it directly to malloc/realloc/free with the same
results.
What have I done wrong?
Jim
#import <Cocoa/Cocoa.h>
static CFAllocatorRef _originalDefaultAllocator;
static void *debug_alloc(CFIndex size, CFOptionFlags hint, void *info)
{
return CFAllocatorAllocate(_originalDefaultAllocator, size, hint);
}
static void *debug_realloc(void *oPtr, CFIndex size, CFOptionFlags
hint, void *info)
{
return CFAllocatorReallocate(_originalDefaultAllocator, oPtr, size,
hint);
}
static void debug_dealloc(void *ptr, void *info)
{
return CFAllocatorDeallocate(_originalDefaultAllocator, ptr);
}
static CFIndex debug_preferred_size(CFIndex size, CFOptionFlags hint,
void *info)
{
return CFAllocatorGetPreferredSizeForSize(_originalDefaultAllocator,
size, hint);
}
static CFAllocatorRef CreateDebugAllocator(CFAllocatorRef alloc)
{
_originalDefaultAllocator = CFAllocatorGetDefault();
CFAllocatorContext context = {0, NULL, NULL, NULL, NULL,
debug_alloc, debug_realloc, debug_dealloc, debug_preferred_size};
CFAllocatorRef debugAllocator = CFAllocatorCreate(alloc, &context);
return debugAllocator;
}
#pragma mark -
int main(int argc, const char *argv[])
{
CFAllocatorRef allocator = CreateDebugAllocator(kCFAllocatorDefault);
CFAllocatorSetDefault(allocator);
return NSApplicationMain(argc, argv);
}
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden