discuss-gnustep
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Swizzling Alloc


From: Gustavo Tavares
Subject: Swizzling Alloc
Date: Sun, 30 May 2021 12:59:42 -0400
User-agent: Cyrus-JMAP/3.5.0-alpha0-468-gdb53729b73-fm-20210517.001-gdb53729b

Hi All,

So—I am trying to use swizzling for my first ever and my goal is to swizzle `alloc`. Why? I want to run a unqiued counter of where my objects are allocated by analyzing the call stack symbols. Sort of like Valgrind so that I can see where my program is leaking data—but I figured since I don't know how to parse Valgrind Objective-C might be an easier way to get something very similar.

Unfortunately when I follow this guide to swizzle the methods—I am getting a segmentation fault. https://newrelic.com/blog/best-practices/right-way-to-swizzle

He basically tell me to do: 

        id gtkDebugAlloc(id self, SEL cmd) { .... } 

        {
             Method nsObjectAllocMethod = class_getClassMethod([NSObject class], @selector(alloc));
             IMP orginalAlloc = method_setImplementation(nsObjectAllocMethod,(IMP)gtkDebugAlloc);
        }

Instead of:

        @interface GTKDebugObject
        @end
        @implementation GTKDebugObject
        +(id)alloc { id toReturn = [self allocWithZone:NSDefaultMallocZone()]; if (toReturn { ... } return toReturn;  }
        @end
       
        {
        
             Method nsObjectAllocMethod = class_getClassMethod([NSObject class], @selector(alloc));
             Method gtkObjectAllocMethod = class_getClassMethod([GTKDebugObject class], @selector(alloc));

      method_exchangeImplementations(nsObjectAllocMethod, gtkObjectAllocMethod);

       
      }


Both methods immediatley give me a segmentation fault when I run this.

Not really sure where to begin debugging this given it's my first time ever swizzling something.

Another approaoch would be to have my own build of `Foundation`. I am probably going to do that next—but this would be a much handier tool if I didn't have to rebuild my system everytime I wanted to debug my mistakes. So far, I don't really know how to tell GNUmake to use another version of Foundation.

It seems like I can change my Foundation library for cross-compiling but not the location of Foundation lookup. 

         . /usr/GNUstep/System/Library/Makefiles/GNUstep-reset.sh
         export LIBRARY_COMBO=ng-gnu-gnu
         . /usr/GNUstep/System/Library/Makefiles/GNUstep.sh
Ref: http://manpages.ubuntu.com/manpages/impish/man7/library-combo.7.html

Seeing also: http://www.gnustep.org/resources/documentation/make_1.html#SEC33

Would appreciate the help as I go off and bang my head on this. Might have a very easy solution. 

Thank for any help in advance,

G


reply via email to

[Prev in Thread] Current Thread [Next in Thread]