Link to home
Start Free TrialLog in
Avatar of deepakgupta
deepakgupta

asked on

When combining a C project with MFC, malloc causing problems in release mode???

Hi All,

I wanted to make GUI windows for a console project written that is in C. So I created a simple MFC Dialog Box Application and included all the console project's source files, header files and the library files in to the workspace and called the main(...) function of the console project on the click of a button by passing the command line arguments with it.

The actual C console project, when run in Microsoft Development Editor in both debug as well as release mode, runs perfectly.

If I run the MFC project by pressing F5 in debug mode, it runs well but causes problem when i run the debug EXE from windows explorer. It also crashes when i run it in release mode either by pressing F5, Ctrl F5 or directly run the release EXE from windows explorer.

The problem when i debugged the release mode was that at a certain point, one malloc was returning NULL despite the fact that I have enough memory (256MB) with me.  

The malloc is being called from the well running C project file. The same malloc call returns good values for 6 times but returns NULL for the seventh call when i try to allocate just 13 bytes. The problem is not the memory shortage but as i think may be the linking of the C project in MFC environment.

Any help is welcomed...
Deepak Gupta.
Avatar of sreenu_v78
sreenu_v78

ru clearing the memory for  every destruction of ur objects?
 
Do you call AfxWinInit from main function?
Create Console Application with MFC support using Application Wizard and move your code to it. Or at least read the code generated by Wizard, maybe you missed something creating your application.
malloc doesn't fail easy usually.
Are you sure you pass 13 as an argument?
Hard-wire 13, recompile and try again.
May it be that you are passing what you think SHOULD be 13.
If you do something like malloc(-13), for example, that would fail, as this is too much memory to allocate.

I'd replace all your malloc callso with calls to some funcion like, then see what is going on from the output

void* MyMalloc(size_t size)
{
   TRACE("malloc(%u)\n", size);
   return malloc(size);
}
Avatar of deepakgupta

ASKER

Hi All,

Thanks a lot for showing so much concern to my question. Actually I have just found the solution for my problem. I am feeling so stupid about my wrong doing, i can't explain. There was nothing wrong with malloc which was returning NULL. Actually what i did, was allocated just one byte to the malloc for a char* variable and storing many chars to it. This malloc was totally different of that was failing and returning NULL. When i allocated actual no. of bytes required for the char* variable, using str.GetLength(), everything got in it's place. Every version started running.

But one thing i want to ask, why did the program not crashed during DEBUG run? No matter how many times i run the same function using the malloc to char*. May be Debug version is so smart to handle all the memory allocations in a Superb Way!!!

Thanks to all again...
Deepak Gupta.
ASKER CERTIFIED SOLUTION
Avatar of nik2k
nik2k

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hi nik,

whatever u r saying is ok. but as the solution of the problem is found ultimately by myself, and i m new to this mailing board, i don't know, whether i should give u points or not. what r the rules in such a case? pls help me.

deepak.
Sorry, I have no idea what you're supposed to do with the points.  I'm new to this board too.