Skip to content

Commit

Permalink
Screen/OSX: Disable use of most Cocoa autorelease pools.
Browse files Browse the repository at this point in the history
Most of them seem no longer needed, so to simplify Objective-C memory debugging, disable their use.
So far, this doesn't improve anything, nor does it make anything worse.
  • Loading branch information
Mario Kleiner authored and kleinerm committed Dec 2, 2024
1 parent 910abb6 commit ccd5cd1
Showing 1 changed file with 34 additions and 34 deletions.
68 changes: 34 additions & 34 deletions PsychSourceGL/Source/OSX/Screen/PsychCocoaGlue.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ PsychError PsychCocoaCreateWindow(PsychWindowRecordType *windowRecord, int windo
windowRecord->targetSpecific.nsuserContext = NULL;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Initialize the Cocoa application object, connect to CoreGraphics-Server:
// Can be called many times, as redundant calls are ignored.
Expand Down Expand Up @@ -181,7 +181,7 @@ PsychError PsychCocoaCreateWindow(PsychWindowRecordType *windowRecord, int windo
PsychMakeRect(windowRecord->globalrect, clientRect.origin.x, screenRect[kPsychBottom] - (clientRect.origin.y + clientRect.size.height), clientRect.origin.x + clientRect.size.width, screenRect[kPsychBottom] - clientRect.origin.y);

// Drain the pool:
[pool drain];
//[pool drain];

// Return window pointer, packed into an old-school Carbon window ref:
*outWindow = (void*) cocoaWindow;
Expand All @@ -195,7 +195,7 @@ psych_bool PsychCocoaMetalWorkaround(PsychWindowRecordType *windowRecord)
__block NSWindow *cocoaWindow;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Define size of client area - the actual stimulus display area:
NSRect windowRect = NSMakeRect(0, 0, (int) PsychGetWidthFromRect(windowRecord->rect), (int) PsychGetHeightFromRect(windowRecord->rect));
Expand Down Expand Up @@ -225,7 +225,7 @@ psych_bool PsychCocoaMetalWorkaround(PsychWindowRecordType *windowRecord)
});

// Drain the pool:
[pool drain];
//[pool drain];

return(TRUE);
}
Expand All @@ -242,7 +242,7 @@ void PsychCocoaGetWindowBounds(void* window, PsychRectType globalBounds, PsychRe
NSWindow* cocoaWindow = (NSWindow*) window;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

DISPATCH_SYNC_ON_MAIN({
// Query and translate content rect of final window to a PTB rect:
Expand All @@ -259,7 +259,7 @@ void PsychCocoaGetWindowBounds(void* window, PsychRectType globalBounds, PsychRe
});

// Drain the pool:
[pool drain];
//[pool drain];
}

pid_t GetHostingWindowsPID(void)
Expand All @@ -272,7 +272,7 @@ pid_t GetHostingWindowsPID(void)
psych_bool verbose = (PsychPrefStateGet_Verbosity() > 5) ? TRUE : FALSE;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

CFArrayRef windowList = CGWindowListCopyWindowInfo(kCGWindowListOptionOnScreenOnly | kCGWindowListExcludeDesktopElements, kCGNullWindowID);
if (!windowList) goto hwinpidout;
Expand Down Expand Up @@ -338,7 +338,7 @@ pid_t GetHostingWindowsPID(void)
hwinpidout:

// Drain the pool:
[pool drain];
//[pool drain];

if (found) {
if (verbose) printf("TARGETWINDOWNAME: '%s' with pid %i.\n", winName, pid);
Expand All @@ -352,7 +352,7 @@ pid_t GetHostingWindowsPID(void)
void PsychCocoaSetUserFocusWindow(void* window)
{
// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];
DISPATCH_SYNC_ON_MAIN({
NSWindow* focusWindow = (NSWindow*) window;

Expand Down Expand Up @@ -390,7 +390,7 @@ void PsychCocoaSetUserFocusWindow(void* window)
});

// Drain the pool:
[pool drain];
//[pool drain];
}

// PsychCocoaGetUserFocusWindow is a replacement for Carbon's GetUserFocusWindow() function.
Expand All @@ -399,15 +399,15 @@ void* PsychCocoaGetUserFocusWindow(void)
__block NSWindow* focusWindow = NULL;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Retrieve pointer to current keyWindow - the window that receives
// key events aka the window with keyboard input focus. Or 'nil' if
// no such window exists:
DISPATCH_SYNC_ON_MAIN({focusWindow = [[NSApplication sharedApplication] keyWindow];});

// Drain the pool:
[pool drain];
//[pool drain];

return((void*) focusWindow);
}
Expand All @@ -417,7 +417,7 @@ void PsychCocoaDisposeWindow(PsychWindowRecordType *windowRecord)
NSWindow *cocoaWindow = (NSWindow*) windowRecord->targetSpecific.windowHandle;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

DISPATCH_SYNC_ON_MAIN({
if (windowRecord->specialflags & kPsychExternalDisplayMethod) {
Expand Down Expand Up @@ -451,15 +451,15 @@ void PsychCocoaDisposeWindow(PsychWindowRecordType *windowRecord)
});

// Drain the pool:
[pool drain];
//[pool drain];
}

void PsychCocoaShowWindow(void* window)
{
NSWindow* cocoaWindow = (NSWindow*) window;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Bring to front:
DISPATCH_SYNC_ON_MAIN({[cocoaWindow orderFrontRegardless];});
Expand All @@ -468,7 +468,7 @@ void PsychCocoaShowWindow(void* window)
DISPATCH_SYNC_ON_MAIN({[cocoaWindow display];});

// Drain the pool:
[pool drain];
//[pool drain];
}

psych_bool PsychCocoaSetupAndAssignOpenGLContextsFromCGLContexts(void* window, PsychWindowRecordType *windowRecord)
Expand All @@ -478,7 +478,7 @@ psych_bool PsychCocoaSetupAndAssignOpenGLContextsFromCGLContexts(void* window, P
GLint opaque = 0;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Enable opacity for OpenGL contexts if underlying window is opaque:
if ([cocoaWindow isOpaque] == true) opaque = 1;
Expand Down Expand Up @@ -522,7 +522,7 @@ psych_bool PsychCocoaSetupAndAssignOpenGLContextsFromCGLContexts(void* window, P
});

// Drain the pool:
[pool drain];
//[pool drain];

// Assign final window globalRect (in units of points in gobal display space)
// and final true backbuffer size 'rect' (in units of pixels):
Expand All @@ -537,47 +537,47 @@ void PsychCocoaSendBehind(void* window)
NSWindow* cocoaWindow = (NSWindow*) window;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Move window behind all others:
DISPATCH_SYNC_ON_MAIN({[cocoaWindow orderBack:nil];});

// Drain the pool:
[pool drain];
//[pool drain];
}

void PsychCocoaSetWindowLevel(void* window, int inLevel)
{
NSWindow* cocoaWindow = (NSWindow*) window;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Set level of window:
DISPATCH_SYNC_ON_MAIN({[cocoaWindow setLevel:inLevel];});

// Drain the pool:
[pool drain];
//[pool drain];
}

void PsychCocoaSetWindowAlpha(void* window, float inAlpha)
{
NSWindow* cocoaWindow = (NSWindow*) window;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Set Window transparency:
DISPATCH_SYNC_ON_MAIN({[cocoaWindow setAlphaValue: inAlpha];});

// Drain the pool:
[pool drain];
//[pool drain];
}

void PsychCocoaSetThemeCursor(int inCursor)
{
// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

DISPATCH_SYNC_ON_MAIN({
switch(inCursor) {
Expand All @@ -600,7 +600,7 @@ void PsychCocoaSetThemeCursor(int inCursor)
});

// Drain the pool:
[pool drain];
//[pool drain];
}

// Variable to hold current reference for App-Nap activities:
Expand Down Expand Up @@ -659,7 +659,7 @@ void PsychCocoaPreventAppNap(psych_bool preventAppNap)
void PsychCocoaGetOSXVersion(int* major, int* minor, int* patchlevel)
{
// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Initialize the Cocoa application object, connect to CoreGraphics-Server:
// Can be called many times, as redundant calls are ignored.
Expand All @@ -674,7 +674,7 @@ void PsychCocoaGetOSXVersion(int* major, int* minor, int* patchlevel)
if (patchlevel) *patchlevel = version.patchVersion;

// Drain the pool:
[pool drain];
//[pool drain];
}

/* Return a pointer to a static string containing the full name of the logged in user */
Expand All @@ -683,14 +683,14 @@ char* PsychCocoaGetFullUsername(void)
static char fullUserName[256] = { 0 };

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

NSString* nsname = NSFullUserName();
const char *srcname = [nsname UTF8String];
strncpy(fullUserName, srcname, sizeof(fullUserName) - 1);

// Drain the pool:
[pool drain];
//[pool drain];

return(fullUserName);
}
Expand All @@ -703,20 +703,20 @@ double PsychCocoaGetBackingStoreScaleFactor(void* window)
NSWindow* cocoaWindow = (NSWindow*) window;

// Allocate auto release pool:
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Set Window transparency:
sf = (double) [cocoaWindow backingScaleFactor];

// Drain the pool:
[pool drain];
//[pool drain];

return (sf);
}

void PsychCocoaAssignCAMetalLayer(PsychWindowRecordType *windowRecord)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
//NSAutoreleasePool *pool = [[//NSAutoreleasePool alloc] init];

// Second time CAMetalLayer reattach: Called from SCREENOpenWindow() after initial
// OpenGL setup, display of the welcome splash screen, startup tests and timing
Expand All @@ -740,7 +740,7 @@ void PsychCocoaAssignCAMetalLayer(PsychWindowRecordType *windowRecord)
}

// Drain the pool:
[pool drain];
//[pool drain];
}

#pragma clang diagnostic pop

0 comments on commit ccd5cd1

Please sign in to comment.