iphone - Callback method before deallocating any Object -


is there way call method before deallocating nsobject class object.

or

is possible writing custom dealloc method nsobject class that
can call method before deallocating object?

as garbage collector not available iphone, wants create small framework handles memory leak @ runtime & create log files leaks (i known there instrument identify leaks still r&d , don't want implement garbage collector algo).

we trying maintain list of allocated object.

for example:

a *a=[[a alloc]init];  nsstring * veribaleaddress=[nsstring stringwithformat:@"%p",&a];  nsstring *allocatedmemoryaddress=[nsstring stringwithformat:@"%p",a];  // global dictionary maintaining list of object  nsmutabledictionary *objects;   [objects setvalue: allocatedmemoryaddress forkey: veribaleaddress]; 

but when object deallocate want 1st look, whether address of object present in dictionary or not. if address present remove dictionary.

please guide me, whether it's possible or not.

thanks

here’s example gist showing how swizzle dealloc method, if that’s after. main part of code:

void swizzle(class c, sel orig, sel patch) {     method origmethod = class_getinstancemethod(c, orig);     method patchmethod = class_getinstancemethod(c, patch);      bool added = class_addmethod(c, orig,         method_getimplementation(patchmethod),         method_gettypeencoding(patchmethod));      if (added) {         class_replacemethod(c, patch,             method_getimplementation(origmethod),             method_gettypeencoding(origmethod));         return;     }      method_exchangeimplementations(origmethod, patchmethod); }  id swizzleddealloc(id self, sel _cmd) {     // …whatever…     return self; }  const sel deallocsel  = @selector(dealloc); // if using arc, try: //  const sel deallocsel  = nsselectorfromstring(@"dealloc");  const sel swizzledsel = @selector(swizzleddealloc); class_addmethod(c, swizzledsel, (imp) swizzleddealloc, "@@:"); swizzle(c, deallocsel, swizzledsel); 

as bavarious says, dark magic , wouldn’t use in production, ever.


Comments

Popular posts from this blog

c# - how to write client side events functions for the combobox items -

exception - Python, pyPdf OCR error: pyPdf.utils.PdfReadError: EOF marker not found -