Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
void dshowCapture::initlogfile()
{
    /////////log file initialization////    
    TCHAR dwDirectory[256];
    TCHAR capturelogfile[256];
    ZeroMemory(dwDirectory, sizeof(dwDirectory));
    ZeroMemory(capturelogfile, sizeof(capturelogfile));
    GetCurrentDirectory(256, dwDirectory);
    strcpy (capturelogfile,dwDirectory);	
    strcat(capturelogfile,"\\CaptureLog.txt");

    std::ofstream *fs;
    fs=new	std::ofstream(capturelogfile); 

    char *log="helow";

    for(int i=0;i<1000;i++)
    fs->write(log,sizeof(log));

    fs->close();
    delete fs;

}


Is this correct implementation because program runs but after
C++
std::ofstream *fs;
fs=new	std::ofstream(capturelogfile);

near about 48 bytes and after execution of for loop 50 bytes increase

after delete operation memory remains to that increased value(near abt 100 bytes)
Posted
Updated 29-Jan-12 5:01am
v2

1 solution

Hi,

Though I have a number of comments to the code like making TCHAR arrays and using strcpy in stead of _tcscpy or bettery yet _tcscpy_s and apparently you are ok wiht writing the size of a pointer (good thing you're not using 64 bits in this case) to the file; I can't directly see a memory leak appearing.

Note that if your app is leaking you will most likely see a "dumping normal block" notification in the output window when closing the app.

Cheers,

AT.
 
Share this answer
 
Comments
pranav_30 29-Jan-12 10:39am    
thx shal i use
Char instead of Tchar..
Ok i wl replace that strcpy

And after terminatng program output winow is ok means no mem leak detected..

Any further sugetn plz..
Mohibur Rashid 29-Jan-12 11:06am    
the difference between char and wchar_t (TCHAR) is char is single byte and wchar_t is two bytes long.
pranav_30 29-Jan-12 11:39am    
ok thanks... so i think i dont required to use TCHAR ..
i replace it with char :)
Addy Tas 29-Jan-12 16:59pm    
Make sure to replace the sizeof(log) by a strlen(log); you're now writing the sizeof pointer to the file.
Addy Tas 29-Jan-12 17:08pm    
Almost forgot; the TCHAR can be either one or two bytes of size, this depends on if you have _UNICODE defined. In the VS project settings you have a entry for use TCHAR as....
If you use TCHAR in one place, make sure that all the manipulation calls (strlen etc.) reflect the same.

Cheers, AT

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900