Re: Zeroing out instance variables
Re: Zeroing out instance variables
- Subject: Re: Zeroing out instance variables
- From: David Hoerl <email@hidden>
- Date: Sat, 17 Apr 2010 09:05:25 -0400
This probably takes more time than allocating a new object :-)
My company uses it to scramble memory for invalidated objects that hang
around for a while, to force crashes if accessed (id object addresses
will be bogus).
David
---
void clearIvars(id obj)
{
unsigned int outCount = 0;
Class class = [obj class];
size_t len = class_getInstanceSize(class);
// no single ivar can exceed the total object ivar size
void *memBlock = malloc(len); // could make static object
memset(memBlock, 0, len);
Ivar *ivars = class_copyIvarList(class, &outCount);
for(unsigned int idx=0; idx < outCount; ++idx, ++ivars)
{
const char *name = ivar_getName(*ivars);
//fprintf(stderr, "Clear ivar %s\n", name);
//if(!strcmp(name, "some-name")) // in my case need to skip one
object_setInstanceVariable(obj, name, memBlock);
}
free(memBlock);
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden