[comp.sys.mac.programmer] "bus error" on GetPort

brown-t@carp.cis.ohio-state.edu (ted croft brown) (11/06/90)

I have come across a probelem that has me REALLY confused.  I am programming
in THINK C 4.02.  I am in the process of writting my first Macintosh C
program (and it's been *educational*).

Anyway, in writting  Modal dialogs I do...

HandleMenuCommand()
{
	WindowPtr window

.
.
	GetPort(window);
	DoDialogRoutine();
	SetPort(window);
}

My code runs at home on my Mac SE (1 MB) but not on the
IIci (8MB, 80MBHD, Multifinder) here at the lab.
I ran the debugger, and I get a "bus error" at the 
	GetPort(window);  

I set a break point before the GetPort and right after it and it 
"bus error"`s  when I tell it to step to the GetPort.  The WindowPtr is 
not NULL (it consistently came up as AC40000 or something like that I 
can`t remember of the top of my head.  I use the same lines elsewhere and
the WindowPtr there came up as AC000000.


Any idea what might be wrong?  The code works fine on my SE.  I changed the
local window variable to a global gWindow and now it works perfectly on the
SE and the IIci.  It's not so much as it won't work, but I don't 
understand why it works one way and not the other.  The SAME code lines 
work elsewhere in the program on BOTH machines.  
I feel like I must have misunderstood something very basic here and would 
appreciate any help, thoughtfull insight, gratutious donations of money to help ease the mental anguish...


Sorry about wasting the bandwith..but I am CONFUSED.

I hope I have included enough information to get some usefull advise.
I will take help/advise any way I can get it (net/email/etc etc)

Ted Brown

phils@chaos.cs.brandeis.edu (Phil Shapiro) (11/06/90)

In article <85692@tut.cis.ohio-state.edu> brown-t@carp.cis.ohio-state.edu (ted croft brown) writes:
   I have come across a probelem that has me REALLY confused.  I am programming
   in THINK C 4.02.  I am in the process of writting my first Macintosh C
   program (and it's been *educational*).

   Anyway, in writting  Modal dialogs I do...

   HandleMenuCommand()
   {
	   WindowPtr window

   .
   .
	   GetPort(window);
	   DoDialogRoutine();
	   SetPort(window);
   }

What you really want to do here is:

    GetPort(&window);

Since GetPort() expects a VAR parameter, you must pass the WindowPtr
by address.  The reason it worked on some computers is because you
were storing your WindowPtr to a random place in memory that happened
to be even and (apparently) unused.

You might want to check out "MacProto.h" header file, it contains
prototypes for most Mac Toolbox calls, and would catch mistakes like
the above.  It's available from most ftp sites.

	-phil
--
   Phil Shapiro                           Technical Support Analyst
   Language Products Group                     Symantec Corporation
		Internet: phils@chaos.cs.brandeis.edu

stevec@Apple.COM (Steve Christensen) (11/07/90)

hairston@henry.ece.cmu.edu (David Hairston) writes:
>[brown-t@carp.cis.ohio-state.edu (ted croft brown) writes:]
>[] I have come across a probelem that has me REALLY confused.  I am
>[] programming in THINK C 4.02.  I am in the process of writting my first
>[] Macintosh C program (and it's been *educational*).
>[]
>[] Anyway, in writting  Modal dialogs I do...
>[]
>[] HandleMenuCommand()
>[] {
>[]  	WindowPtr window
>[]
>[] .
>[] .
>[]  	GetPort(window);
>[]  	DoDialogRoutine();
>[]  	SetPort(window);
>[] }
>[]
>[] My code runs at home on my Mac SE (1 MB) but not on the
>
>hmm, altho taking the address of a pointer might seem strange at
>first thought, you need to pass the address of "window" to GetPort,
>as in:	GetPort(&window);
>where it's value will get set correctly.  don't know why this
>would work on one machine and not another (seems like the example
>should always fail).

On Macs prior to the IIci, RAM addresses were allowed to wrap even though
there was no physical RAM filling up the entire space.  Starting with the
IIci, the MMU is setup so that you can only read/write to the actual RAM,
and if you go past it, it'll bus error on you.  More than likely, the
"pointer" that's being passed to GetPort falls into that range of RAM
addresses that don't really exist...

steve
-- 
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 whoami?     Steve Christensen
 snail:      Apple Computer, 20525 Mariani Ave, MS-81CS, Cupertino, CA  95014
 Internet:   stevec@apple.com
 AppleLink:  stevec
 CompuServe: 76174,1712