ios - Segmentation fault OR Stacktrace when using UIView with message forwarding feature of Objective-C -
the problem: given class inherits nsobject , has instance of uiview wrapped. class named uiviewwrapper. use uiviewwrapper e.g. 'addsubview' of uiview. uiviewwrapper should act 'normal' uiview class.
i utilizing "message forwarding" feature of objective-c try things working. in 'addsubview' calls made instance of uiviewwrapper , point messages forwarded correctly wrapped instance of uiview. program crashes either 'segmentation fault: 11' (memory problems) or stacktrace (see below).
my questions: possible @ all? if yes, 'message forwarding' best way it? if yes, missing here? if no, what's better approach solve kind of problem?
the code (omitting main.m , appdelegate.h since have usual contents):
//appdelegate.m @implementation appdelegate - (bool) application : (uiapplication *) application didfinishlaunchingwithoptions : (nsdictionary *) launchoptions { self.window = [[uiwindow alloc] initwithframe : [[uiscreen mainscreen] bounds]]; self.window.backgroundcolor = [uicolor whitecolor]; //a normal uiview -> works expected //uiview *theview = [[uiview alloc] initwithframe : cgrectmake(50, 50, 50, 50)]; //[theview setbackgroundcolor : [uicolor yellowcolor]]; //the wrapped version -> fails when added rootview uiviewwrapper *theview = [[uiviewwrapper alloc] init]; uiview *rootview = [[uiview alloc] init]; [rootview addsubview : theview]; uiviewcontroller *rootviewcontroller = [[uiviewcontroller alloc] init]; [rootviewcontroller setview : rootview]; self.window.rootviewcontroller = rootviewcontroller; [self.window makekeyandvisible]; return yes; } //uiviewwrapper.h #import <uikit/uikit.h> @interface uiviewwrapper : nsobject @end //uiviewwrapper.m @interface uiviewwrapper() @property(nonatomic, strong) uiview *uiview; @end @implementation uiviewwrapper - (nsmethodsignature *) methodsignatureforselector : (sel) selector { return [uiview instancemethodsignatureforselector : selector]; } -(void) forwardinvocation : (nsinvocation *) forwardedinvocation { if (!self.uiview) { self.uiview = [[uiview alloc] initwithframe : cgrectmake(50, 50, 50, 50)]; [self.uiview setbackgroundcolor : [uicolor redcolor]]; } [forwardedinvocation invokewithtarget : self.uiview]; } @end
the important parts of stacktrace appears sometimes. other time segmentation fault: 11
jan 27 13:55:09 dirks-macbook-air showcase[15741]: +[__nsblockvariable__ superlayer]: unrecognized selector sent class 0x2df82b4 jan 27 13:55:09 dirks-macbook-air showcase[15741]: *** terminating app due uncaught exception 'nsinvalidargumentexception', reason: '+[__nsblockvariable__ superlayer]: unrecognized selector sent class 0x2df82b4' *** first throw call stack: ( 0 corefoundation 0x0253fa14 __exceptionpreprocess + 180 1 libobjc.a.dylib 0x028a9e02 objc_exception_throw + 50 2 corefoundation 0x02548c03 +[nsobject(nsobject) doesnotrecognizeselector:] + 275 3 corefoundation 0x024866bd ___forwarding___ + 1037 4 corefoundation 0x0248628e _cf_forwarding_prep_0 + 14 5 uikit 0x00dce092 -[uiview(internal) _addsubview:positioned:relativeto:] + 1452 6 uikit 0x00dbc8e4 -[uiview(hierarchy) addsubview:] + 56 7 showcase 0x001dcb4b -[appdelegate application:didfinishlaunchingwithoptions:] + 343
maybe if clarify trying solve, can more. have feeling there's no point in finding solution , choosing different approach better. case in objective-c. better stick "cocoa way" of doing things.
basically, you're calling addsubview
method object of incorrect class argument. see picture #1. , that's reason crash.
Comments
Post a Comment