• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 298
  • Last Modified:

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.
0
deepakgupta
Asked:
deepakgupta
1 Solution
 
sreenu_v78Commented:
ru clearing the memory for  every destruction of ur objects?
 
0
 
AlexFMCommented:
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.
0
 
nik2kCommented:
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);
}
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
deepakguptaAuthor Commented:
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.
0
 
nik2kCommented:
Debug version of malloc is different from Release.
It has a lot of memory use tracking stuff and depending on your settings, it can also pad your arrays to detect overrides.  This is why memory overrides sometimes don't cause access violation in Debug mode.
0
 
deepakguptaAuthor Commented:
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.
0
 
nik2kCommented:
Sorry, I have no idea what you're supposed to do with the points.  I'm new to this board too.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now