[MSX-C] Q&A official thread

Page 63/68
56 | 57 | 58 | 59 | 60 | 61 | 62 | | 64 | 65 | 66 | 67 | 68

Par Grauw

Ascended (10768)

Portrait de Grauw

28-01-2023, 19:01

rolandve wrote:

Some times I get heap overflows from the ascii C compiler. I've got no clue how I actually fix them, sometimes they simply go away by placing a routine somewhere else. What is the compiler telling me and why? Moving code shouldn't solve an error.....

Heap means free memory where it can allocate space with malloc, and heap overflow would suggest that you try to allocate more space than is available. Perhaps you can increase the amount of heap space to reserve with a command line option?

Par rolandve

Champion (358)

Portrait de rolandve

29-01-2023, 09:14

@Grauw, thanks for the explaination.
Now I have something that is puzzling. I use ldirvm and ldirmv to move pieces fro VRAM to a buffer and back and write data to this block. To make these operations fast, I use disscr() and enascr(). That works, you don't see it happening. However when I try to move this piece of the screen (restore -> store next part, draw -> restore -> store next part, draw ) my screen starts to jump like moving all characters down and then up again. I have tried di() and ei() but that doesn't make difference. Is there a know thing that can do this when accessing vram with ldirvm and ldirmv?

Par rolandve

Champion (358)

Portrait de rolandve

29-01-2023, 11:32

example of heap overflow.. I am allocating nothing. When I remove this code it compiles and functions just fine.
While trying to debug I removed every line, removed all pointers and work directly with the struct. Just this function having content causes a heap overflow.

VOID rWin( dx, dy )
int dx,dy;
{
    wStruct *ptr;
    char num= rtWin.active; 
    
    if ( win[num].X == 0 ) return;
    ptr = &win[num];
    fVRAM((int)ptr->X,(int)ptr->Y,(int)ptr->W,(int)ptr->H,(int)num);
    ptr->W+=dx;
    ptr->H+=dy;
    if ( ((ptr->X) + (ptr->W) ) > 79 ) ptr->W-=1;
    if ( ((ptr->Y) + (ptr->H) ) > 25 ) ptr->H-=1;
    tVRAM((int)ptr->X,(int)ptr->Y,(int)ptr->W,(int)ptr->H,(int)num);
    drawWin((int)ptr->X,(int)ptr->Y,(int)ptr->W,(int)ptr->H,(int)ptr->A);
}

The compiler says the heap overflow happens in the code below

ptr = &win[rtWin.active];
if ( ptr->X > 0) {
         fVRAM((int)ptr->X,(int)ptr->Y,(int)ptr->W,(int)ptr->H,(int)rtWin.active);
         dWin(ptr);
         kilbuf();
                    }

Par wbahnassi

Master (192)

Portrait de wbahnassi

29-01-2023, 11:04

You mean you get heap overflow while compiling the code? or when actually executing your code? Big difference..

Par rolandve

Champion (358)

Portrait de rolandve

29-01-2023, 11:33

wbahnassi wrote:

You mean you get heap overflow while compiling the code? or when actually executing your code? Big difference..

during compilation.

Par ~mk~

Champion (328)

Portrait de ~mk~

30-01-2023, 04:49

rolandve wrote:

The compiler says the heap overflow happens in the code below

ptr = &win[rtWin.active];
if ( ptr->X > 0) {
         fVRAM((int)ptr->X,(int)ptr->Y,(int)ptr->W,(int)ptr->H,(int)rtWin.active);
         dWin(ptr);
         kilbuf();
                    }

Please copy and paste the error. The compiler should tell exactly in which line number the error occurred.
Heap overflow could also happen with complex expressions.
What is fVRAM? Is it a function or a macro definition?
In either case, why not pass just ptr as parameter instead of X, Y, W, H?

Par rolandve

Champion (358)

Portrait de rolandve

30-01-2023, 07:57

Well, that's the "not so much fun part". The compilers output points at the line with fVRAM you see above, see the code below. So you are looking at the line that caused the heap overflow because the compiler said it was that line. I solved this compilation error by moving the fVRAM function to another location in the source file and it works again. Insane yes. Passing the ptr seems logical, but in this case, the code becomes less readable.

VOID fVRAM( sx, sy, sw, sh, num )
int sx,sy,sh,sw,num;
{
    
    int lus=0;
    TINY *buffer;
    unsigned base = 0x2000 + (num * 0x870);
    int it=0;
    
    if ( ( buffer = alloc(sw)) == 0 ) {
        puts("memory error");
        exit(0);
    }
    memset(buffer,(char)0,sw);
    sw+=2;
    sh+=2;
    disscr();
    for (lus= sy; lus <= (sy+sh-1) ; lus++ )
    {
        ldirmv(buffer,base+(it * sw),sw);
        ldirvm((((lus-1)*80)+sx),buffer,sw);
        it++;
        
    }
    disscr();
}

Par pizzapower

Master (167)

Portrait de pizzapower

31-01-2023, 20:16

You have to free the memory after you allocate and use it. Put a free(buffer); at the end of the function. Or next time you try to allocate more, it may fail for lack of free available space.

Par rolandve

Champion (358)

Portrait de rolandve

01-02-2023, 11:43

pizzapower wrote:

You have to free the memory after you allocate and use it. Put a free(buffer); at the end of the function. Or next time you try to allocate more, it may fail for lack of free available space.

Good observation!

Par rolandve

Champion (358)

Portrait de rolandve

02-02-2023, 22:36

So how to use Jiffy. The system starts to behave erratic, like hang.

I have Jiffy defined as

#define Jiffy 	(*(unsigned *)0xFC9E)

while debugging I also defined Jiffy as

#define Jiffy 	(*(TINY *)0xfc9e)

But when I want to work with Jiffy, the system hangs, when I use it like below. It works exactly 1 time. Then the system hangs. Reset required.

 unsigned oldJiffy;
oldJiffy = Jiffy;
if ( Jiffy > (oldJiffy +10 ) ) {
update();
oldJiffy = Jiffy;
}

Taking out Jiffy, the systems loops extremely fast, so it's not the code itself. Why does using Jiffy hang a system?

Page 63/68
56 | 57 | 58 | 59 | 60 | 61 | 62 | | 64 | 65 | 66 | 67 | 68