[comp.sys.mac.programmer] AARRGG! Need help with OpenPicture

dtani@bbn.com (Dan Tani) (11/07/88)

Hi, 
I've got a programming problem that is driving me up the wall. I am
programming in LSC 3.0 and I want to be able to export some drawings
via the clipboard.  I wrote this subroutine and I use all the standard
include files.  First, here's the code:

PicToClip()
{
	Rect		testRect;
	extern PicHandle	ClipPic;

	SetRect(&testRect,0,0,200,200);
	ClipPic = OpenPicture(&testRect);
	HLock(ClipPic);
	/* my drawing calls*/
	ClosePicture();
	ZeroScrap();
	PutScrap(GetHandleSize(ClipPic),'PICT', *ClipPic);
	HUnlock(ClipPic);
	KillPicture(ClipPic);
}

The problem is that OpenPicture(&testRect) doesn't change ClipPic  -
it returns a handle of 0x0000 which crashes everything.  The Rect gets
set correctly, and I even tried putting a ClipRect(&testRect) right
before the OpenPicture call like IM 1 suggests - and got even more
problems!  That returned an odd address error right at the ClipRect
call.

This seems very simple.  The PutScrap part works great - I had it
write a PICT from a resource, and everything worked fine. 

Also, are there any other things I should look out for while drawing
picture?  I assume that any SetPorts should be avoided, but I'm just
calling some routines that I use to draw to the screen and to the
printer.

Any help is appreciated!  I'll post a summary if there is interest.

Thanks in advance,

Dan Tani
DTANI@BBN.COM

jkjl@munnari.oz (John Lim) (11/08/88)

In article <31922@bbn.COM>, dtani@bbn.com (Dan Tani) writes:
> set correctly, and I even tried putting a ClipRect(&testRect) right
> before the OpenPicture call like IM 1 suggests - and got even more
> problems!  That returned an odd address error right at the ClipRect
> call.
> 
Sounds as if you have an invalid or corrupt grafport if you crash
in ClipRect(). Dont be too confident that the locality of the crash
is also the cause of the crash ! 

Did you do a DisposWindow() just before the ClipRect() ? Check 
thePort using the debugger and make sure everything is ok. Obviously
OpenPicture is only a symptom and not the problem, if you crash in
ClipRect(), before the OpenPicture().

	john
	happy moth-killing :-)