(Link to AcmlmWiki) Offline: thank ||bass
Register | Login
Views: 13,040,846
Main | Memberlist | Active users | Calendar | Chat | Online users
Ranks | FAQ | ACS | Stats | Color Chart | Search | Photo album
06-16-24 03:57 PM
0 users currently in Programming.
Acmlm's Board - I3 Archive - Programming - calloc() returns already-allocated pointer New poll | |
Add to favorites | Next newer thread | Next older thread
User Post
HyperHacker

Star Mario
Finally being paid to code in VB! If only I still enjoyed that. <_<
Wii #7182 6487 4198 1828


 





Since: 11-18-05
From: Canada, w00t!
My computer's specs, if anyone gives a damn.
STOP TRUNCATING THIS >8^(

Last post: 6328 days
Last view: 6328 days
Posted on 07-15-06 08:20 PM Link | Quote
For some reason, after creating a few instances of a class (which calls calloc() in its creation), a couple malloc()s, and lots and lots of realloc()s, calloc() starts returning pointers to already-allocated memory. To be specific:

Start with a struct SHAPE3D, which among other things points to an array of structs POINT3D:
typedef struct {
Vector Position;
COLOUR Colour;
GLfloat TX, TY; //Texture coords
bool Highlighted;
unsigned char Check[16]; //DEBUG
} POINT3D;


typedef struct {
unsigned int NumPoints;
POINT3D* Point;
GLfloat NormX, NormY, NormZ; //'Normal' coords (points outward from the face)
} SHAPE3D;


1) malloc a buffer about 100KB, read some data into it.
2) realloc two buffers: Shape (array of SHAPE3D) and Point (array of POINT3D).
4) For each new element in Shape, set Shape[x].Point to various elements of Point.
5) create an instance of the class, pass it Shape and the number of elements in Shape.
6) the class's constructor callocs an array of SHAPE3D and POINT3D, and copies the data from Shape and the POINT3D structs pointed to within.
7) Repeat steps 2 through 6 about 30 times.

What happens is after creating 7 of these class instances, on the 8th attempt, when it tries to calloc() an array of SHAPE3D, the pointer it gets back doesn't point to free memory! It points to Shape[0].Point instead. Making Point a large static/malloc()ed array and never realloc()ing it prevents this problem, but the size of the array needed varies, so this solution is no good (and seems to be more of a band-aid fix than anything).

The one odd thing I notice about the problematic realloc() call is that sometimes it returns addresses around 0x11000000-0x12FFFFFF, and other times they're around 0x00030000-0x0003FFFF. This happens every time when creating the 8th class instance, which is when things go wrong, but it also happens when creating the first which works fine. I even tried doing a manual realloc() - malloc() a new buffer, copy the old one into it, and free the old one - it still returns these ranges, and although calloc() doesn't return that one specific pointer anymore, something still gets messed up and the program crashes trying to read Shape[0].Point.

It seems like some memory corruption is screwing up the realloc()/calloc() calls, but there's no allocation functions being used before this, nor even anything being written to an array. The only thing I can see that could cause a problem is reading data from a file in step 1, but disabling that doesn't fix it.

[edit] I tried not creating any instances of the class at all, and just doing the same calloc() that the class constructor would do. It didn't change anything.

[reedit] Using malloc() or realloc() in place of calloc() doesn't help, and also, I wrote a quick function to test allocation:
void AllocTest(int n)
{
void* P[16];
void* A;

int i, j, k;
for(i=0; i<16; i++)
{
A = calloc(sizeof(SHAPE3D), 120);
if(!A)
{
DebugOut(DO_MAJOR | DO_NO_ERROR | DO_NO_MSGBOX, "Alloc %d failed on check %d\n", i, n);
for(k=0; k MK64UnloadLevel();
free(LevelObject);
exit(1);
}

for(j=0; j {
if(A == P[j])
{
DebugOut(DO_MAJOR | DO_NO_ERROR | DO_NO_MSGBOX, "Alloc %d error on check %d\n", i, n);
for(k=0; k MK64UnloadLevel();
free(LevelObject);
exit(1);
}
}
P[i] = A;
}

for(k=0; k<16; k++) free(P[k]);
}

I call this in 8 different places before doing the stuff I described above. It doesn't fail, and there's almost no memory corruption - calloc() returns a valid pointer, and the program runs, but there's still a few things corrupted. Again, using malloc() instead in this function doesn't change anything.

And damn that [i] tag.


(edited by HyperMackerel on 07-15-06 08:58 PM)
(edited by HyperMackerel on 07-15-06 09:29 PM)
(edited by HyperMackerel on 07-15-06 09:30 PM)
neotransotaku

Sledge Brother
Liberated from school...until MLK day








Since: 11-17-05
From: In Hearst Field Annex...

Last post: 6330 days
Last view: 6327 days
Posted on 07-16-06 01:43 AM Link | Quote
what compiler are you using? check to see the documentation for malloc(), realloc(), and calloc() to see what it does.

Also, it appears your calloc() call is backwards. According to the man page for calloc, it says that calloc is void *calloc(size_t nelem, size_t elsize);, which says your calloc call for the code below should be calloc(120, sizeof(SHAPE3D));
HyperHacker

Star Mario
Finally being paid to code in VB! If only I still enjoyed that. <_<
Wii #7182 6487 4198 1828


 





Since: 11-18-05
From: Canada, w00t!
My computer's specs, if anyone gives a damn.
STOP TRUNCATING THIS >8^(

Last post: 6328 days
Last view: 6328 days
Posted on 07-16-06 05:09 AM Link | Quote
Hm, it is too, but that wasn't the problem. (x*y == y*x, so it doesn't matter much. ) Just a design error:

2) realloc two buffers: Shape (array of SHAPE3D) and Point (array of POINT3D).
4) For each new element in Shape, set Shape[x].Point to various elements of Point.


Notice the problem here? This is in a loop, so after taking the address of an element of Point, Point then gets realloc()'d, and that address is no longer valid. I just had it take the array element number instead, and set the pointers at the end of the loop.
neotransotaku

Sledge Brother
Liberated from school...until MLK day








Since: 11-17-05
From: In Hearst Field Annex...

Last post: 6330 days
Last view: 6327 days
Posted on 07-16-06 02:34 PM Link | Quote
1) Do you malloc space for SHAPE3D.Point? Because saying "For each new element in Shape, set Shape[x].Point to various elements of Point" means you are using SHAPE3D.Point as an array (i.e. you want several points). If you don't malloc an array for it, you can only hold 1 point in that struct.

2) After you set some points into Shape[x].Point, do you realloc your Point3D buffer? If you do, then anything you set in Shape[x].Point via Point[y] is now useless because Shape[x].Point now points to garbage.
HyperHacker

Star Mario
Finally being paid to code in VB! If only I still enjoyed that. <_<
Wii #7182 6487 4198 1828


 





Since: 11-18-05
From: Canada, w00t!
My computer's specs, if anyone gives a damn.
STOP TRUNCATING THIS >8^(

Last post: 6328 days
Last view: 6328 days
Posted on 07-16-06 03:34 PM Link | Quote
1) Yes.
2) Yes, I said that was the problem. It's working now.
neotransotaku

Sledge Brother
Liberated from school...until MLK day








Since: 11-17-05
From: In Hearst Field Annex...

Last post: 6330 days
Last view: 6327 days
Posted on 07-16-06 03:44 PM Link | Quote
It's working now.

what I get for sleeping at 2am and waking up at 6am for several night straight...
HyperHacker

Star Mario
Finally being paid to code in VB! If only I still enjoyed that. <_<
Wii #7182 6487 4198 1828


 





Since: 11-18-05
From: Canada, w00t!
My computer's specs, if anyone gives a damn.
STOP TRUNCATING THIS >8^(

Last post: 6328 days
Last view: 6328 days
Posted on 07-16-06 07:34 PM Link | Quote
Actually there is still a problem, and other OpenGL programs such as Project64 have it too. Whenever I call glReadPixels(), plus a few times when OpenGL is initializing, every translucent window on the same monitor as my GL window flashes. It's a pain since I like to keep my taskbar translucent and I use this function every time the mouse is moved to see if a polygon is being pointed to.

And if anyone knows a way to make XP not BSOD when I apply translucency or colour-key transparency to an OpenGL window, that'd be a nice bonus.
Add to favorites | Next newer thread | Next older thread
Acmlm's Board - I3 Archive - Programming - calloc() returns already-allocated pointer |


ABII

Acmlmboard 1.92.999, 9/17/2006
©2000-2006 Acmlm, Emuz, Blades, Xkeeper

Page rendered in 0.033 seconds; used 391.27 kB (max 477.82 kB)