• 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: Forcing subclass creation through method swizzling not airtight?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Forcing subclass creation through method swizzling not airtight?


  • Subject: Re: Forcing subclass creation through method swizzling not airtight?
  • From: Antonio Nunes <email@hidden>
  • Date: Mon, 12 Mar 2012 20:20:34 +0000

On 12 Mar 2012, at 16:57, Greg Parker wrote:

> On Mar 10, 2012, at 11:05 PM, Antonio Nunes wrote:
>> In the latest public release of my software, I used method swizzling to force creation of my subclass when the system tries to create instances of its superclass. However, I have received a very few reports where apparently instances of the superclass are still created, and wreak havoc (i.e. they cause a crash). In what way is it still possible for instances of the superclass to be created?
>>
>> + (void)initialize
>> {
>> 	// To force PDFDocuments to be created as NMD_PDFDocuments
>> 	// Necessary because of an issue with PDFThumbnailView
>> 	Method originalMethod = class_getClassMethod([PDFDocument class], @selector(allocWithZone:));
>> 	Method superMethod = class_getClassMethod(class_getSuperclass([PDFDocument class]), @selector(allocWithZone:));
>>
>> 	Method replacedMethod = class_getClassMethod([PDFDocument class], @selector(allocWithZoneReplacement:));
>> 	if (superMethod == originalMethod) {
>> 		class_addMethod(object_getClass([PDFDocument class]), @selector(allocWithZone:),
>> 						method_getImplementation(replacedMethod),
>> 						method_getDescription(replacedMethod)->types);
>> 	}
>>
>> 	// To force PDFPages to be created as NMD_PDFPage
>> 	originalMethod = class_getClassMethod([PDFPage class], @selector(allocWithZone:));
>> 	superMethod = class_getClassMethod(class_getSuperclass([PDFPage class]), @selector(allocWithZone:));
>>
>> 	replacedMethod = class_getClassMethod([PDFPage class], @selector(allocWithZoneReplacement:));
>> 	if (superMethod == originalMethod) {
>> 		class_addMethod(object_getClass([PDFPage class]), @selector(allocWithZone:),
>> 						method_getImplementation(replacedMethod),
>> 						method_getDescription(replacedMethod)->types);
>> 	}
>> }
>
> Note that your code does nothing if PDFPage or PDFDocument themselves override +allocWithZone:. Do you know whether that may be the case?

I don't think that is the case. The code works fine almost always. If the superclasses themselves overrode +allocWIthZone: I suppose my code would never run, right? I think I've only seen a single source with a few crash reports where PDFPages had been created instead of NMD_PDFPages. So, doesn't that suggest that my code works (at least most of the time)?

>> The swizzling is performed early on during the application startup:
>
> If you want to be absolutely sure your swizzling is performed early on during application startup, use a +load method instead of a +initialize method.

OK, I've change it to a +load method. We'll see if I no longer receive crash reports referencing misbehaving PDFPages.

Thanks Greg,
António

-----------------------------------------------------------
And could you keep your heart in wonder
at the daily miracles of your life,
your pain would not seem less wondrous
than your joy.

--Kahlil Gibran
-----------------------------------------------------------




_______________________________________________

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


References: 
 >Forcing subclass creation through method swizzling not airtight? (From: Antonio Nunes <email@hidden>)
 >Re: Forcing subclass creation through method swizzling not airtight? (From: Greg Parker <email@hidden>)

  • Prev by Date: Re: Method Replacement (was "swizzling") not airtight?
  • Next by Date: iOS: preventing popping nav controller
  • Previous by thread: Re: Method Replacement (was "swizzling") not airtight?
  • Next by thread: Transition from [NSEvent phase] to [NSEvent momentumPhase]
  • Index(es):
    • Date
    • Thread