Advertisement

PLEASE Help with malloc()!!

Started by July 28, 2000 09:27 PM
10 comments, last by TheGecko 24 years, 4 months ago
I''m sooo pissed of right now it''s not even funny! I''ve been trying to figure out this problem for 2 nights now and still no solution! Let me show you: Assume I have the following class and structures:

//---------------------------------------------------------------------------------
typedef struct PALETTE
{
	BYTE Red;
	BYTE Green;
	BYTE Blue;

}Palette;
//---------------------------------------------------------------------------------
typedef struct BFFHEADER
{
	WORD	BFF_ID;
	DWORD	Width;
	DWORD	Height;
	WORD	BPP;
	WORD	CompressionType;
	DWORD	BufferSize;
	PALETTE palette[256];
	UCHAR	*Buffer;

}BFFHeader;
//---------------------------------------------------------------------------------
class BFF32API CBFFImage  
{
public:
	BFFHeader *Image;

public:
    BFFRESULT	BFF_LoadImage(LPCTSTR Filename);//Loads a BFF image
    //so on and so forth
};
//---------------------------------------------------------------------------------
 
now take this implementation of the load function:

BFFRESULT CBFFImage::BFF_LoadImage(LPCTSTR Filename)
{
	//This function loads a BFF file specified
	//in the Filename parameter and does any 
	//necessary decompression to the image
	//if any exists.

	Image = (BFFHeader*)malloc(sizeof(BFFHeader));//<---NOT FREAKING WORKING!!!			
///blah blah blah

 
when I run it through the debugger,it crashes with an unhandled exception at the line where I''m allocating my memory! What the hell is happening?! Could someone PLEASE answer this for me since it''s been a big pain in my backside for the last 2 days!
I don''t see anything wrong, except that since your using C++ you should be using new and not malloc().

I threw this in a newly created VC++ 6.0 Win32 Application created as a typical Hello, World! program. It worked and executed in both debug and release.

// just below #define MAX_LOADSTRINGclass PALETTE{	public:		BYTE mA;		BYTE mB;		BYTE mC;};class HEADER{	public:		WORD	mD;		DWORD	mE;		DWORD	mF;		WORD	mG;		WORD	mH;	    WORD	mI;		PALETTE mJ;		UCHAR	*mK;};class IMAGE {	public:		HEADER *ptr;	public:		int load(void);		int load2(void);};// globals sectionIMAGE	i1, i2;// first action in WinMain()i1.load();i2.load2();// at bottomint IMAGE::load(void){	ptr = new HEADER;	return(0);}int IMAGE::load2(void){	ptr = (HEADER*) malloc(sizeof(HEADER));	return(0);} 


Its not exactly the same, but similar. Maybe a bit more C++ like (drop load2() though ).

Maybe its not your code, but your build settings. Good luck.

Mike Roberts
aka milo
mlbobs@telocity.com
Advertisement
That didn''t even work!WTF?!

Ok,I tried another way and that was to declare the pointer to a BFFHeader structure IN the BFF_LoadImage(...) function and then allocate memory to it using the "new" operator as well as the malloc function and it worked fine.Now I''m totally confused.I mean it should work even if I declared a BFFHeader pointer as a class variable and not a local variable to a member function.

If it''s something wrong with my build settings,anyone have any idea where I should start looking? I''m using VC++ 6.0.

I know what you are talking about. I have the same problem is you. It should not matter if you are using malloc or new.. but to be on the safe side try and construct some test code with the .c extension and see what happens...

Good Luck

The nightmare travels across the cosmos with his burning mane. The trail of ash that is produced.

?Have a nice day!?

Did you forget to allocate memory for the Buffer?

I did...still not woking

And the most f****d thing about all this is that it worked with another project of mine perfectly and I was doing the exact same thing.That''s why I''m convinced that something else is wrong.Maybe it is my build settings but I took a look at that just now and there''s no difference.

I''ve made a temporary fix for all this but it''s not that efficient.If anyone figures out how to fix this please let me know.Thanx
Advertisement
Well, sometimes if you overrun a buffer in a part of your program, malloc will poop out and cause errors. Check all of your code for potential buffer overruns (I know, this is a major pain in the ass).

Does it crash on the first iteration of the function, or only on all subsequent iterations? If only on all subsequent ones, perhaps your load function overwrites valuable unallocated memory.

Hope this helps.

-e
"Only two things are infinite, the universe and human stupidity, and I'm not sure about the former." - Albert Einstein
do you also remember to allocate memory when loading the picture into the buffer ??
UCHAR *Buffer
Ries
I''m not sure if it is even possible, but could it be that the class CBFFImage isn''t allocated when you call the load function?
I mean, I think the compiler translates a call to
CBFFImage *MyImage;
MyImage->BFF_LoadImage("Filename") to
CBFFImage::BFF_LoadImage(MyImage,"Filename") (or something like that)
so my point is, maybe what''s blowing things up is the attribution to the Image member, not the malloc part.

Just ignore me if you allocated the class before calling the function, please.

You know, I never wanted to be a programmer...

Alexandre Moura
I checked for buffer overruns and still nothing.

Claus: Yes I did allocate memory for the *Buffer pointer. That''s already been taken care of.

alexmoura:It wouldn''t let me! When I declare CBFFImage *MyImage as a class public in my main class,and then I go to allocate memory using the "new" operator,it still crashes! At that line! I''m now totally at a loss as to why this is happening

This topic is closed to new replies.

Advertisement