In Cocoa, I get the array of windows of an app using the following code:
CFArrayRef windows;
AXError gettingWindowsResult = AXUIElementCopyAttributeValues(app, (CFStringRef)NSAccessibilityWindowsAttribute, 0, 999, &windows);
Then I check some values of those windows and keep the AXUIElementRef of one of them in a variable of my class. At the end of the method, I release the CFArrayRef to make sure I don’t have any memory leaks:
if (windows != nil)
{
CFRelease(windows);
}
Though this makes it so that when I try to use the window I kept, I get a bad access error. So my question: is it necessary that I release the array? And if so, how do I prevent the bad access error?
Why not make a retained copy of the “
AXUIElementRef” of the one element you want to keep?To do this, figure out the index of the element you want to keep, and then make another call to the “
AXUIElementCopyAttributeValues” function, only this time just pass the index for the element you desire and a “maxValue” of 1. For example, for the element at position 26:Then you can safely call “
CFRelease” on the “windows” array.