NSObject PerformSelector Issue
Hooooookay so here's another "I just have no idea what's going on" problem:
The first time I make a call to getFullWL()
below, I get all my values as expected. Each subsequent call, however, returns nan
instead of the true value(-nan(0x400000000)
in XCode or something)
Furthermore, if I put my "debug" lines in SFLog
the value prints as nan
but returns the correct value! If I comment out SFLog
(which is just a #define for NSLog
depending on a level I set - nothing special), then the value is not caught by isnan()
in View
but is checked as isnan()
in Window
Window
and View
are sitting on one thread, while DataModule
and DCMPix
are sitting on another (main thread, I believe). DCMPix
also gets some data from a Core-Data object (fImage
I believe).
void Window::PopUp()
{
// Grab the View from the Pipe Thread and cast it to my local namespace subclass View
View *view = static_cast< View* >(getPipe()->getView(event.context.view));
float fullww = view->getFullWW();
float fullwl = view->getFullWL();
//SFLog(@"WindowLevel Window %f", wl);
// set SLider Values
if (!isnan(fullwl)){
[[vc winLevel] setMinValue:fullwl];
[[vc winLevel] setMaxValue:(-1.0f*fullwl)];
}
}
float View::getFullWL()
{
float wl = _callPixMethod(@selector(fullwl)).floatValue;
if ( isnan(wl) ) wl = _wl * 2.0f; //<-- is never detected as NaN here
//SFLog(@"WindowLevel View %f", wl); //<-- but is *printed* as NaN here
return wl;
}
// A Union to "cast" the return from the [object performSelector:] method.
// Since it returns `id` and (float)(id) doesn't work.
union performReturn {
id OBJC_ID;
int intValue;
float floatValue;
bool boolValue;
};
performReturn View::_callPixMethod(SEL method)
{
DataModule* data;
DataVisitor getdata(&data);
getConfig()->accept(getdata);
performReturn retVal;
retVal.OBJC_ID = data->callPixMethod(_datasetIndex, _datasetID, method);
return retVal;
}
id DataModule::callPixMethod(int index, std::string predicate, SEL method)
{
DCMPix *pix =[[getSeriesData(predicate) pixList_] objectAtIndex:index];
pthread_mutex_lock(&_mutex);
id retVal = [pix performSelector:method];
pthread_mutex_unlock(&_mutex);
return retVal;
}
// DCMPix.m (from API, can't change)
- (float开发者_如何学Go) fullwl
{
if( fullww == 0 && fullwl == 0) [self computePixMinPixMax];
return fullwl;
}
- (void)computePixMinPixMax
{
float pixmin, pixmax;
if( fImage == nil || width * height <= 0) return;
[checking lock];
@try
{
if( isRGB)
{
pixmax = 255;
pixmin = 0;
}
else
{
float fmin, fmax;
vDSP_minv ( fImage, 1, &fmin, width * height);
vDSP_maxv ( fImage , 1, &fmax, width * height);
pixmax = fmax;
pixmin = fmin;
if( pixmin == pixmax)
{
pixmax = pixmin + 20;
}
}
fullwl = pixmin + (pixmax - pixmin)/2;
fullww = (pixmax - pixmin);
}
@catch (NSException * e)
{
NSLog( @"***** exception in %s: %@", __PRETTY_FUNCTION__, e);
}
[checking unlock];
}
Help! Why would I not be getting the correct value in Window
?
I also get no nan
's when calling view->getFullWW()
even though it follows the same execution path. (but calls @selector(fullww)
)
No Errors or exceptions are thrown. Just that bizarre behavior.
Thanks!
After further testing, the following change makes all the difference:
float View::getFullWL()
{
float wl = _callPixMethod(@selector(fullwl)).floatValue;
if ( isnan(wl) ) wl = _wl * 2.0f; //<-- is never detected as NaN here
NSLog(@"WindowLevel View %f", wl); //<-- is *printed* as NaN here
return wl; //<-- returns expected value
}
float View::getFullWL()
{
float wl = _callPixMethod(@selector(fullwl)).floatValue;
if ( isnan(wl) ) wl = _wl * 2.0f; //<-- is never detected as NaN here
return wl; //<-- returns `nan`
}
After more testing, it doesn't matter where I put the NSLog() statement, if it occurs before float fullwl =
is assigned.
void Window::PopUp()
{
// Grab the View from the Pipe Thread and cast it to my local namespace subclass View
View *view = static_cast< View* >(getPipe()->getView(event.context.view));
float fullww = view->getFullWW();
NSLog("Putting this here, makes fullwl work. Removing it, stores fullwl as nan");
float fullwl = view->getFullWL();
//SFLog(@"WindowLevel Window %f", wl);
Whelp, Here I am answering my own question again but the answer is... RTFM.
The aSelector argument should identify a method that takes no arguments. For methods that return anything other than an object, use NSInvocation.
So the answer is, use NSInvocation
and not [obj performSelector:]
精彩评论