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

Posted on 2003-02-18
Medium Priority
Last Modified: 2013-11-20
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.
Question by:deepakgupta
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Expert Comment

ID: 7980000
ru clearing the memory for  every destruction of ur objects?
LVL 48

Expert Comment

ID: 7980202
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.

Expert Comment

ID: 7985691
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);
Technology Partners: 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!


Author Comment

ID: 7997465
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.

Accepted Solution

nik2k earned 200 total points
ID: 7999384
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.

Author Comment

ID: 8006413
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.


Expert Comment

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

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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question