• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Grabbing screen - kern bad access
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Grabbing screen - kern bad access


  • Subject: Grabbing screen - kern bad access
  • From: Gábor Németh <email@hidden>
  • Date: Mon, 16 Oct 2006 07:07:40 +0200

Hello,

I am a beginner in the field of Cocoa, so please forgive me if this is a RTFM style question, however i've did a good amount of googling before writing here, so even a pointer to the appropriate place in the manual is appreciated.
I've defined a simple form with an NSImageView and an NSButton. When the button is pressed the program should grab the contents of the screen, and place it in the NSImageView. For further processing, the NSImageview is just a POC control.


The problem is: If i step through the code with the debugger, or have quartz debug active it runs well. If not, the program dies with kern_bad_access.
It seems that initWithFocusedViewRect can not complete because some un-flushed data.
Any idea what should I flush? or what should I do, in order to avoid the crash?


Here is the code :

- (IBAction)doItNow:(id)sender{
screenRect = [[NSScreen mainScreen] frame];
NSImage *screenImg = [[NSImage alloc] initWithSize:NSMakeSize (screenRect.size.width, screenRect.size.height)];
[screenImg setScalesWhenResized:YES];
window = [[NSWindow alloc] initWithContentRect:screenRect
styleMask:NSBorderlessWindowMask backing:NSBackingStoreNonretained
defer:NO];
myView = [[NSView alloc] initWithFrame:screenRect];
[window setLevel:NSScreenSaverWindowLevel + 100];
[window setHasShadow:NO];
[window setAlphaValue:0.0];
[window setContentView:myView];
[window orderFront:self];
float scaleFactor = 0.5;
[myView lockFocus];
NSBitmapImageRep *screenRep= [[NSBitmapImageRep alloc] initWithFocusedViewRect:screenRect];
[screenImg addRepresentation:screenRep];


  [myView unlockFocus];
  NSGraphicsContext *gctxt = [NSGraphicsContext currentContext];
  [gctxt flushGraphics];

  [window flushWindow];
  [window orderOut:self];
  [window close];

NSImage *scaledImage = [[NSImage alloc] initWithSize:NSMakeSize ([screenImg size].width * scaleFactor, [screenImg size].height * scaleFactor)];
[scaledImage lockFocus];
[screenImg drawInRect:NSMakeRect(0,0,[scaledImage size].width, [scaledImage size].height)
fromRect:NSMakeRect(0,0,[screenImg size].width, [screenImg size].height)
operation:NSCompositeCopy fraction:1.0];
[scaledImage unlockFocus];



[imageBox setImage:scaledImage]; }


Thanks in advance, Gabor _______________________________________________ Do not post admin requests to the list. They will be ignored. Cocoa-dev mailing list (email@hidden) Help/Unsubscribe/Update your Subscription: This email sent to email@hidden
  • Follow-Ups:
    • Re: Grabbing screen - kern bad access
      • From: Andrew Farmer <email@hidden>
  • Prev by Date: Re: Approved [:.VIRUS.:]
  • Next by Date: Re: Grabbing screen - kern bad access
  • Previous by thread: Re: Approved [:.VIRUS.:]
  • Next by thread: Re: Grabbing screen - kern bad access
  • Index(es):
    • Date
    • Thread