• 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: Zeroing out instance variables
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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


  • Prev by Date: Re: Zeroing out instance variables
  • Next by Date: Re: Zeroing out instance variables
  • Previous by thread: Re: Zeroing out instance variables
  • Next by thread: Re: Zeroing out instance variables
  • Index(es):
    • Date
    • Thread