discuss-gnustep
[Top][All Lists]
Advanced

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

Re: Swizzling Alloc


From: Fred Kiefer
Subject: Re: Swizzling Alloc
Date: Mon, 31 May 2021 15:48:02 +0200

Hi Gustavo,

if what you want to achieve is finding leaked objects then there is abetter way 
to do so. GNUstep already has a build in leak detection: GSDebugAllocationAdd. 
This registers all allocated objects per class in a table and with the function 
GSDebugAllocationListAll you get an NSString ready to print out, showing the 
current state of this table. Doing so multiple times in you application should 
show you which classes are leaking objects.

And if you actually have a gui application, things are even more simple. Just 
one the about dialog, clock on the displayed icon and you will get a window 
displaying the content of that table. Keep on refreshing and you will see which 
classes move up to the top.

Hope this helps,
Fred

> Am 30.05.2021 um 18:59 schrieb Gustavo Tavares <mucholove@fastmail.com>:
> 
> 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]