Memory Leaks With CString in vc6.0 in Release mode
-
I am facing a run time problem with the CString in Release builds in vc6.0 i observed that the memory wont be released for cstring object i checked in the task manager can any one help for this here i posted the code also #include "stdafx.h" #include "afx.h" #include "afxwin.h" #include <windows.h> #include "fixalloc.h" #pragma warning (disable:4786) #pragma warning (disable:4503) #include<list> #include<string> #include<vector> using namespace std; #include <afxtempl.h> void test() { vector<CString> stldmlist; CString a; FILE *fp=fopen("E:\\log1.txt","r"); char str[1000]; while(!feof(fp)) { fscanf(fp,"%s\n",str); a=str; stldmlist.push_back(a); } fclose(fp); AfxMessageBox("Memory"); } UINT32 main(int argc, char* argv[]) { CString obj; AfxMessageBox("Start "); test(); AfxMessageBox("End"); return 1; }
Hi this Ravinder
-
I am facing a run time problem with the CString in Release builds in vc6.0 i observed that the memory wont be released for cstring object i checked in the task manager can any one help for this here i posted the code also #include "stdafx.h" #include "afx.h" #include "afxwin.h" #include <windows.h> #include "fixalloc.h" #pragma warning (disable:4786) #pragma warning (disable:4503) #include<list> #include<string> #include<vector> using namespace std; #include <afxtempl.h> void test() { vector<CString> stldmlist; CString a; FILE *fp=fopen("E:\\log1.txt","r"); char str[1000]; while(!feof(fp)) { fscanf(fp,"%s\n",str); a=str; stldmlist.push_back(a); } fclose(fp); AfxMessageBox("Memory"); } UINT32 main(int argc, char* argv[]) { CString obj; AfxMessageBox("Start "); test(); AfxMessageBox("End"); return 1; }
Hi this Ravinder
Ramasani wrote:
memory wont be released for cstring object i checked in the task manager
Given that task manager doesn't tell you if objects released memory or not, I presume you were looking at an process's working set... The behaviour I'm guessing you're talking about (the process's working set rising and not falling after
test
exits) is to be expected - when you start allocating memory, the C runtime requests large chunks of memory from the Windows memory manager. It will not necessarily bother releasing them when you release objects, as allocating and releasing memory with the Windows memory manager is quite expensive. -
Ramasani wrote:
memory wont be released for cstring object i checked in the task manager
Given that task manager doesn't tell you if objects released memory or not, I presume you were looking at an process's working set... The behaviour I'm guessing you're talking about (the process's working set rising and not falling after
test
exits) is to be expected - when you start allocating memory, the C runtime requests large chunks of memory from the Windows memory manager. It will not necessarily bother releasing them when you release objects, as allocating and releasing memory with the Windows memory manager is quite expensive. -
if i go for more number of CString calls it returns memory leak errors(no memory) is there any patches available for this? i need very urgent.
Hi this Ravinder
Ramasani wrote:
memory leak errors(no memory)
Ummm - 'no memory' does not necessarily imply memory leaks. It could just be that you're allocating a *lot* of memory. How big is this log file you're reading in?
Ramasani wrote:
is there any patches available for this?
Let's think about this - what's more likely to be at fault - MFC/C++ standard libraries or your code/understanding thereof? Mmmm.
-
Ramasani wrote:
memory leak errors(no memory)
Ummm - 'no memory' does not necessarily imply memory leaks. It could just be that you're allocating a *lot* of memory. How big is this log file you're reading in?
Ramasani wrote:
is there any patches available for this?
Let's think about this - what's more likely to be at fault - MFC/C++ standard libraries or your code/understanding thereof? Mmmm.
Actually i am using CString in my application more than 10k times and my apllication will run for days in this cases memory is not releasing thats the problem i have given a sample code only but it is working fine in Debug mode not in release mode ?
Hi this Ravinder
-
Actually i am using CString in my application more than 10k times and my apllication will run for days in this cases memory is not releasing thats the problem i have given a sample code only but it is working fine in Debug mode not in release mode ?
Hi this Ravinder
Ramasani wrote:
it is working fine in Debug mode not in release mode
In what sense? You have not stated a) what you expected to observe when running in release mode and b) what actually did happen. I've run your sample code (admittedly built with VS2003 rather than VC6) and monitored the execution of both debug and release variants with task manager and Process Explorer[^]. In both cases, it behaved exactly as I would expect.
-
Ramasani wrote:
it is working fine in Debug mode not in release mode
In what sense? You have not stated a) what you expected to observe when running in release mode and b) what actually did happen. I've run your sample code (admittedly built with VS2003 rather than VC6) and monitored the execution of both debug and release variants with task manager and Process Explorer[^]. In both cases, it behaved exactly as I would expect.
-
Actually i am using CString in my application more than 10k times and my apllication will run for days in this cases memory is not releasing thats the problem i have given a sample code only but it is working fine in Debug mode not in release mode ?
Hi this Ravinder
Ramasani wrote:
...my apllication will run for days in this cases memory is not releasing...
Are you expecting this to happen automatically? Nowhere in your code are you directly calling
new
ordelete
. It seems the problem and the code snippet you've shown are not related."Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"The brick walls are there for a reason...to stop the people who don't want it badly enough." - Randy Pausch