malloc trouble?

BlitzMax Forums/BlitzMax Programming/malloc trouble?

Warner(Posted 2011) [#1]
In my engine, I've added a c file that renders a ROAM terrain.
However, it gives me trouble, and I could really use some help.

There was a complaint that it gives an error in 'debug' mode.
I've seen this error before, when testing the engine on another PC. There, the error suddenly dissapeared after I recompiled the sample.

I've installed MinGW 5.1.3 to be able to recreate this error, hopefully to solve it. Now, I can't seem to recreate the situation where it was working.

In release mode, the program runs. Also, when I add "GCSetMode 2", the program runs, too. So I think it could be caused by the GC.
I've heard once that combining malloc and GC could give trouble?

Strangely enough, the complaint was, that the error was caused by a newer version of MinGW, and reverting to 5.1.3 solved the issue. Also, my suggestion to add 'GCSetMode 2' didn't work.

The C code creates a 'roam' struct:
    rm=(roam)malloc(sizeof(roam_struct));


And within that struct, an array of 'roamdm' is created
     rm->dmstore=(roamdm)malloc(rm->dmstoren*sizeof(roamdm_struct));


The error appears on somewhat strangest points, for instance in a 'ResizePixmap' instruction that follows the roam creation routine.

It is difficult to pinpoint the exact error location when it appears in the C file, because the debugger doesn't seem to trace in there.

If there is anyone who could shed some light on this?
If needed, the engine mod can be downloaded here:
http://code.google.com/p/warner-engine/downloads/list


ima747(Posted 2011) [#2]
Are you compiling with threading enabled or not? Blitz uses a different GC routine with threading vs. non threading compiles. THe multi-threaded GC has given me plenty of headaches in the past. If you don't need threading keep it off, it will make life easier.

Are you freeing the malloced memory at any point, or is it intended to be held for some later time? Are you sure you have enough memory for the malloc and the following mallocs etc. from resizepixmap?

odd that disabling the garbage collector would still fail, though again, are you sure you have enough memory to malloc (including the things blitz is going to malloc for you like with a resizepixmap)...


Czar Flavius(Posted 2011) [#3]
Stabbing in the dark, but don't you need to cast malloc to a pointer (roam*) and not just the struct type? Or is roam a typedef for a pointer?

Last edited 2011


Warner(Posted 2011) [#4]
Oh, man .. I feel so stupid now. I was looking into the creation part a bit better, because of what you said:
    rm->size=0;
    rm->heights=NULL;

    rm=(roam)malloc(sizeof(roam_struct));

:S Must have been tired when I did that. I was using the 'roam' before creating it. It seems to work now, I'll run some tests.

So I guess you were on the right track: it seems I was using non-alloced memory.

'Roam' is no pointer, it is a struct. The function first creates a struct, then later converts it to a 'roamhandle', which is indeed a typedef for a pointer. I didn't write the terrain code, my C skills are much limited, so I just took the way it is built for granted.

Thanks for your input. I was really on a dead end.


outsider(Posted 2011) [#5]
'Roam' is no pointer, it is a struct.

File roamtypes.h line 43:
typedef struct roam_structdef roam_struct,*roam;

what's means that roam is a pointer but we used it before memory allocation for him.

Anyway I also have not seen this before, good job :)

Last edited 2011


Czar Flavius(Posted 2011) [#6]
malloc returns a pointer to new bland memory region, which you need to cast to a pointer to the data type you are using. In pure C, I think the cast itself is optional (you can just malloc straight to the pointer) but perhaps good practice. As outsider points out, roam is a pointer to roam_structdef.

It's been a long time since I did pure C (in C++ the struct but without a typedef is optional), but I think the long form equivalent containing cast is:

rm=(struct roam_structdef *)malloc(sizeof(struct roam_structdef));


Last edited 2011