[comp.lang.c] Help needed with Turbo C++ 1.0 memory allocation

cooper@plains.NoDak.edu (Jeff Cooper) (10/02/90)

I'm having a problem in Turbo C and I'm hoping someone can help me 
out.  I'm trying to compile using the compact model and I'm having
trouble allocating data.  What I'm trying to do is allocate enough 
memory to store the 4 bit planes from an EGA graphics screen.  I have 
a structure set up as follows:

struct bitmap
{
    unsigned char far *plane[4];
};

This should give me an array of four far pointers, each pointing to
an area of memory that will contain one bit plane.  Next I allocate
memory with the following routine:

int GetImageMemory(struct bitmap *img) 
{
    long area;
    register int i,j;

    /* bytes for one bitplane */
    area = (long) width * height / 8; 
    for (i = 0; i < 4; i++) {
        if ((img->plane[i] = (char *) farmalloc (area)) == NULL) {
            for (j = 0; j < i; j++)
                farfree(img->plane[j]);
            return(OUT_OF_MEMORY);
        } 
    } 
    return(OK);
} /* GetImageMemory */

I call this routine like this:

void ShowImage(int x1, int y1, char *name)
{
    struct bitmap *image;
    register int i;
    int error;

    if ((error = DisplayGif(name)) != OK) 
       ShutDown(error);
    if ((error = GetImageMemory(image)) != OK)
       ShutDown(error);
    GetImage(0,0,0,width,height,image);
    PutImage(1,x1,y1,width,height-1,img);
    for(i = 0; i < 4; i++)
       farfree(image->plane[i]);
}


GetImage gets an bitmap from the given page and coordinates and stores 
it in 'image', PutImage is just the reverse, it writes to a given page
and coordinates.  When I'm done writing, I free up the memory used
to store the image, although this is just for testing.  When I have 
the program running, I plan to keep about 8 of these images in memory 
at once (about 50K each for the size of image that I am using).  

The problem is that this will lock up the machine, for some reason, from
what I've been able to debug, farfree doesn't seem to free the memory.
It doens't happen right away either, the first two passes through
'ShowImage' work fine, it's the third one that fails.  I inspected 
'image' right after if came back from 'GetImageMemory' and it looked
like:

plane[0]  67c4:0004                 Core left
plane[1]  6acc:0004                 229,296K
plane[2]  6dd4:0004
plane[3]  70dc:0004

I'm typing this up from notes I made during th execution, so it's not
exactly what the inspect looks like, but it gives you the general idea.
Then it should free the memory, and read in the next image.  After the
second pass through 'ShowImage', 'image' looks like:

plane[0]  70dc:0004                 Core left
plane[1]  73e4:0004                 192,048K
plane[2]  76ec:0004
plane[3]  79f4:0004

The differance between the two memory measurements is 37,248K, which
is almost exactly the amount of memory it take to store 3 bit planes,
(37,000)  If you're wondering the width and height of each image is
496x200.  

Does anyone have any clue as to what I'm doing wrong?  I'm using 
Turbo C++ 1.0 (I've installed the first set of patches to it).  Please
email any answers/ideas to me.  

Thank you for any help anyone can offer!

----

"I have something to say, It's better to burn out than fade away"
 - The Kurgan, "Highlander"

Jeff Cooper                        USnail address:
cooper@plains.nodak.edu            1444 32nd St. SW, Apt 318
(701)235-6495                      Fargo, ND  58103