Welcome, Guest. Please login or register.

Author Topic: "Close Window" tutorial errors  (Read 3935 times)

Description:

0 Members and 2 Guests are viewing this topic.

Offline Sidewinder

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show all replies
    • http://www.liquido2.com
Re: "Close Window" tutorial errors
« on: May 10, 2003, 08:25:11 PM »
@cycloid

Quote

well i've scoured the "Amiga C Tutorial" website at liquido2.com for an email link but cant find one, so i'll post it here. i got my first amiga window runnig using the source but found some "issues" with the code that needed debugging, here they are:


Thanks for the feedback.  Actually my email address (amiga@ssmnet.net) is found in the introduction, but I should probably add it somewhere in every tutorial.

Quote

>45 struct IntuiText *MyText;

this fills me with dread, surely that should be:

>45 struct IntuiText *MyText = new IntuiText;

but does that mean i have to "free" it when i shutdown?


Good catch.  This should fill you with dread and is my bad for not catching it in the first place.  You are correct that the memory should be reserved for the IntuiText structure.  The Amiga way of doing this is:

struct IntuiText *MyText = (struct IntuiText *)AllocMem( sizeof( struct IntuiText ), 0L );

And yes, if you allocate  memory in this way you must free it after you are done using it by adding:

FreeMem( MyText, sizeof( struct IntuiText) );

to the end of that block.

An alternative would be to create a real IntuiText structure instead of a pointer to an IntuiText structure:

struct IntuiText MyText;

MyText.LeftEdge = 10;
MyText.TopEdge = 20;
MyText.IText = "Click the close button to exit!";
MyText.DrawMode = JAM1;
MyText.FrontPen = 1;
MyText.NextText = NULL;

PrintIText( MyWindow->RPort, &MyText, 0, 0 );

This is the change I will make to the tutorial source.

Quote

>54 PrintIText( MyWindow->RPort, MyText, 0, 0 );

changing it to this:

>54 PrintIText( MyWindow->RPort, MyText, MyWindow->BorderLeft, MyWindow->BorderTop );

offsets it into the window's window bit. i learnt that by reading intuition.h


Very true, but as mentioned already, adding a WA_GimmeZeroZero tag when defining the window will do the same automagically.

Quote

>104 CloseLibrary( (struct Library *)IntuitionBase );

This caused shell lockups and all sorts of memory trouble, it worked cleanly when i made it:

>104 CloseLibrary( IntuitionBase );


The code in the tutorial is correct for ANSI C compilers such as GCC.  Strictly speaking, CloseLibrary() expects an argument of type struct Library *.  The pointer IntuitionBase is of type struct IntuitionBase * not struct Library * (although the IntuitionBase structure actually begins with a Library structure) and therefore must be cast to a struct Library * when used in CloseLibrary().  Including the protos as mentioned earlier is a good way to catch these type mismatches.  By the way, which compiler are you using?

And yes, I forgot the return 0 at the end of main().  ;-)

Thank you for bringing up these errors.  I'll correct them and publish the updated tutorials.

Also, you might be able to find some very useful documentation near the bottom of  this page.
Sidewinder
 

Offline Sidewinder

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show all replies
    • http://www.liquido2.com
Re: "Close Window" tutorial errors
« Reply #1 on: May 11, 2003, 12:59:58 AM »
@Karlos

No, there is no reason I prefer AllocMem() over AllocVec() other than AllocMem() is a more descriptive function name which helps new programmers understand what is actually happening.  It is also compatable accross all OSes if anyone is really still using OS 1.3.  :-D  But in practice, AllocVec() is prefered.
Sidewinder
 

Offline Sidewinder

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show all replies
    • http://www.liquido2.com
Re: "Close Window" tutorial errors
« Reply #2 on: May 11, 2003, 01:33:28 AM »
@Karlos

Hey, no problem.

You're exactly right, AllocVec() and FreeVec() are very much akin to ANSI C's malloc() and free().  The only difference is that AllocVec() allows you to specify different Amiga specific types of memory such as Chip RAM, Fast RAM, etc.

I'm certain I'll get into this more in one of my tutorials.  Right now I haven't even touched on memory management.   ;-)

Sidewinder
 

Offline Sidewinder

  • Full Member
  • ***
  • Join Date: Mar 2002
  • Posts: 241
    • Show all replies
    • http://www.liquido2.com
Re: "Close Window" tutorial errors
« Reply #3 on: May 11, 2003, 05:59:37 PM »
@cycloid

[quote[
being a migrating winblows C++ coder i'd expect to use malloc (and thus having it auto-freed for me if i'm feeling lazy :) ) but i thought i'd better check first.
[/quote]

:-D  Heh, lazy programmers?  What is this you speak of?  ;-)  Honestly, resource tracking is cool, and malloc() works great for most things on the Amiga such as reserving space for structures, but if you are trying to reserve memory for things like graphics or sound you will need to use the AllocMem() or AllocVec() calls as they allow you explicitly specify that you want to reserve graphics (chip) memory.

Sidewinder