[comp.sys.amiga.tech] Intuition bug in ActivateWindow???

cunniff@hpfcdc.HP.COM (Ross Cunniff) (04/23/89)

There seems to be a bug in the ActivateWindow call.  It is exhibited
by:
	0. Launch the program from an icon
	1. Closing the Workbench screen
	2. Opening a new custom screen
	3. Opening one or more windows on the new screen
		WITHOUT the ACTIVATE flag set.
	4. ActivateWindow() one of those windows.

What seems to be happening is that ActivateWindow doesn't check to see
whether the previously active window is still open, and writes over
random areas of memory, causing all sorts of cute things to happen
(menus are displayed strangely, and sometimes chip memory gets EXCEEDINGLY
fragmented.  Note that this bug seems to crop up with DPaint II every
so often, as well).

At the end of this message is C source to recreate the bug.  It compiles
under Lattice 5.0.

				Ross Cunniff
				Hewlett-Packard Colorado Languages Lab
				...{ucbvax,hplabs}!hpfcla!cunniff
				cunniff%hpfcrt@hplabs.HP.COM

---Cut here-----------------Cut here---------------------Cut here---
#define TWO_WINDOWS		/* for prettier fireworks */

#include <exec/types.h>
#include <intuition/intuition.h>

struct IntuitionBase
    *IntuitionBase;
struct Screen
    *Scr;
struct Window
    *Win;
#ifdef TWO_WINDOWS
struct Window
    *Win2;
#endif

/* Static data defined at the end of the file */
extern struct NewScreen
    NewScr;
extern struct NewWindow
    NewWin;
extern struct Menu
    MenuBar[];

/* Function prototypes (comment out parm info for Manx) */
void
    *OpenLibrary( char *, long );
struct Screen
    *OpenScreen( struct NewScreen * );
struct Window
    *OpenWindow( struct NewWindow * );
void
    *GetMsg( struct MsgPort * );
int
    OpenWorkBench( void ),
    CloseWorkBench( void );
void
    ReplyMsg( void * ),
    ActivateWindow( struct Window * ),
    SetMenuStrip( struct Window *, struct Menu * ),
    ClearMenuStrip( struct Window * ),
    CloseWindow( struct Window * ),
    CloseScreen( struct Screen * ),
    CloseLibrary( void * ),
    Terminate( void ),
    exit( int );



void _main()
{
    struct IntuiMessage
	*Msg;

    /* Open Intuition */
    IntuitionBase = OpenLibrary( "intuition.library", 33L );
    if( !IntuitionBase )	Terminate();

    /* Try to close the workbench */
    if( !CloseWorkBench() )	Terminate();

    /* Open the screen */
    Scr = OpenScreen( &NewScr );
    if( !Scr )			Terminate();

    /* Open the first window */
    NewWin.Screen = Scr;
    Win = OpenWindow( &NewWin );
    if( !Win )			Terminate();
    SetMenuStrip( Win, MenuBar );

#ifdef TWO_WINDOWS
    /* Open the second window */
    NewWin.LeftEdge += 200;
    Win2 = OpenWindow( &NewWin );
    if( !Win2 )			Terminate();
    SetMenuStrip( Win2, MenuBar );
    ActivateWindow( Win2 );
#else
    ActivateWindow( Win );
#endif

    /* Now wait to be done */
    while( 1 ) {
#ifdef TWO_WINDOWS
	Wait((1<<Win->UserPort->mp_SigBit)|(1<<Win2->UserPort->mp_SigBit));
#else
	Wait( 1 << Win->UserPort->mp_SigBit );
#endif
	while( Msg = GetMsg( Win->UserPort ) ) {
	    if( Msg->Class == CLOSEWINDOW ) {
		ReplyMsg( Msg );
		Terminate();
	    }
	    ReplyMsg( Msg );
	}
#ifdef TWO_WINDOWS
	while( Msg = GetMsg( Win2->UserPort ) ) {
	    if( Msg->Class == CLOSEWINDOW ) {
		ReplyMsg( Msg );
		Terminate();
	    }
	    ReplyMsg( Msg );
	}
#endif
    }
}


void Terminate()
{
#ifdef TWO_WINDOWS
    if( Win2 )		CloseWindow( Win2 );
#endif
    if( Win )		CloseWindow( Win );
    if( Scr )		CloseScreen( Scr );
    OpenWorkBench();
    if( IntuitionBase )	CloseLibrary( IntuitionBase );
    exit( 0 );
}


/* Program data goes here (I think this makes it easer to read...) */
struct NewScreen
    NewScr = {
	0, 0, 640, 400, 3,		/* Left, Top, Width, Height, Depth */
	0, 1,				/* DetailPen, BlockPen */
	HIRES, CUSTOMSCREEN,		/* ViewModes, Type */
	NULL, NULL, NULL, NULL		/* Font, Title, Gads, BitMap */
    };
struct NewWindow
    NewWin = {
	50, 50, 100, 100,		/* Left, Top, Width, Height */
	0, 1,				/* DetailPen, BlockPen */
	CLOSEWINDOW,			/* IDCMPFlags */
	WINDOWDRAG | WINDOWCLOSE | WINDOWDEPTH,	/* Flags */
	NULL, NULL, NULL, NULL, NULL,	/* Gad, Chk, Ttl, Scr, BM */
	-1, -1, -1, -1,			/* MinMax Width Height */
	CUSTOMSCREEN
    };

struct IntuiText
    Text[16] = {
	{0,1,JAM2,5,0,NULL,"Item0",NULL}, {0,1,JAM2,5,0,NULL,"Item1",NULL},
	{0,1,JAM2,5,0,NULL,"Item2",NULL}, {0,1,JAM2,5,0,NULL,"Item3",NULL},
	{0,1,JAM2,5,0,NULL,"Item4",NULL}, {0,1,JAM2,5,0,NULL,"Item5",NULL},
	{0,1,JAM2,5,0,NULL,"Item6",NULL}, {0,1,JAM2,5,0,NULL,"Item7",NULL},
	{0,1,JAM2,5,0,NULL,"Item8",NULL}, {0,1,JAM2,5,0,NULL,"Item9",NULL},
	{0,1,JAM2,5,0,NULL,"ItemA",NULL}, {0,1,JAM2,5,0,NULL,"ItemB",NULL},
	{0,1,JAM2,5,0,NULL,"ItemC",NULL}, {0,1,JAM2,5,0,NULL,"ItemD",NULL},
	{0,1,JAM2,5,0,NULL,"ItemE",NULL}, {0,1,JAM2,5,0,NULL,"ItemF",NULL},
    };

struct MenuItem
    Items[16] = {
	{Items+ 1,1, 1,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+0)},
	{Items+ 2,1,11,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+1)},
	{NULL,    1,21,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+2)},
	{Items+ 4,1, 1,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+3)},
	{Items+ 5,1,11,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+4)},
	{Items+ 6,1,21,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+5)},
	{NULL,    1,31,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+6)},
	{Items+ 8,1, 1,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+7)},
	{NULL,    1,11,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+8)},
	{Items+10,1, 1,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+9)},
	{Items+11,1,11,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+10)},
	{NULL,    1,21,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+11)},
	{Items+13,1, 1,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+12)},
	{Items+14,1,11,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+13)},
	{Items+15,1,21,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+14)},
	{NULL,    1,31,170,10,ITEMTEXT|ITEMENABLED|HIGHCOMP,0,(APTR)(Text+15)},
    };

struct Menu
    MenuBar[5] = {
	{MenuBar+1,  0,0,70,10,MENUENABLED,"Menu0",Items+0},
	{MenuBar+2, 70,0,70,10,MENUENABLED,"Menu1",Items+3},
	{MenuBar+3,140,0,70,10,MENUENABLED,"Menu2",Items+7},
	{MenuBar+4,210,0,70,10,MENUENABLED,"Menu3",Items+9},
	{NULL,     280,0,70,10,MENUENABLED,"Menu4",Items+12},
    };

/*** EOF intuibug.c ***/

ugkamins@sunybcs.uucp (John Kaminski) (04/24/89)

The original post dealt with howcome ActivateWindow() does strange things.
in both the algorithm in the explanation and the source code itself, the
WorkBench screen is closed and THEN the custom screen is opened.  Is that
going to be kosher with the system?  Shouldn't those two steps be in
transposed order?

---
a-WYSIWYG, a-WYSIWIG
a-WYSIWYG, a-WYSIWIG
a-WYSIWYG, a-WYSIWIG
a-WYSIWYG, a-WYSIWIG
In the jungle
The silicon jungle
The process sleeps tonight

cunniff@hpfcdc.HP.COM (Ross Cunniff) (04/24/89)

> The original post dealt with howcome ActivateWindow() does strange things.
> in both the algorithm in the explanation and the source code itself, the
> WorkBench screen is closed and THEN the custom screen is opened.  Is that
> going to be kosher with the system?  Shouldn't those two steps be in
> transposed order?

It doesn't matter.  The behavior is exhibited if CloseWorkBench() occurs
after OpenScreen().  It is kinda desirable to CloseWorkBench BEFORE
OpenScreen(), to reduce memory fragmentation.


				Ross Cunniff
				Hewlett-Packard Colorado Languages Lab
				...{ucbvax,hplabs}!hpfcla!cunniff
				cunniff%hpfcrt@hplabs.HP.COM