memory leak

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#include <sys/stat.h>
#include <stdio.h>
#include <tchar.h>
#include <windows.h>
#define WHITESPACE_CHARS " \t\n\r"

#undef streq
#ifdef _UNICODE
#define  streq(x,y) (wcscmp(x,y) == 0)
#else
#define streq(x,y) (strcmp(x,y)==0)
#endif 
void win_path_to_unix_ath(char *path)
{
	char *c;
	for (c = path; *c; c++)
		if (*c == '\\')
			*c = '/';
}

File::File()
{
}

File::~File()
{

}
File::Pointer File::New()
{
	return Pointer(new File());
}

void  File::ScanDir(std::string dir, std::vector<std::string> &filenames)
{
	win_path_to_unix_ath((char*)dir.c_str());
	if (dir[dir.size() - 1] != '/')
		dir += ("/");


	std::wstring windir(dir.begin(), dir.end()), file;

	WIN32_FIND_DATA find;
	HANDLE h;
	DWORD attr;
	bool add;
	h = FindFirstFile((windir + _T("*")).c_str(), &find);
	if (h != INVALID_HANDLE_VALUE)
	{
		do
		{
			if (!streq(find.cFileName, _T(".")) && !streq(find.cFileName, _T("..")))
			{
				file = windir + find.cFileName;
				attr = GetFileAttributes(file.c_str());
				if (attr & FILE_ATTRIBUTE_DIRECTORY)
				{
					std::vector<std::string> temp;
					ScanDir(std::string(file.begin(), file.end()), filenames);
				}
				else
				{
					filenames.push_back(std::string(file.begin(), file.end()));
				}
			}
		} while (FindNextFile(h, &find));
		FindClose(h);
	}
}
#include <iostream>
int main()
{
	bool flag = false;
	for (int i = 0; i < 1000; i++)
	{
		if (flag)
		{
			std::shared_ptr<File> file = File::New();
			std::vector<std::string> files;
			file->ScanDir("D:\\datu_data_test", files);//a local path
		}
	}
	std::cout << "success" << std::endl;
	std::cin.get();
}

//on windows 64, visual studio 2015.
//set flag false/true, when run to 'std::cin.get()', the memory has different size, it is a memory leak?
//who can help?
I guess the memory size is different on win32 and win64 owing to the different architecture...
A program with memory requirements that depend on a flag value uses different memory amounts when the flag value varies? Whodathunkit?
closed account (48bpfSEw)
I think your compiler is clever enough to skip this code:

1
2
3
std::shared_ptr<File> file = File::New();
			std::vector<std::string> files;
			file->ScanDir("D:\\datu_data_test", files);//a local path 


if the flag is set to false.

because the if - condition is always false!


it compiles only that:

1
2
3
4
5
        for (int i = 0; i < 1000; i++)
	{
	}
	std::cout << "success" << std::endl;
	std::cin.get();
Last edited on
@Necip, yeah! If there is no code in 'for' loop, the memory's size is smaller, but it is normal?
I test it:
if code exists and 'flag' = true, the memory's size is 0.6Mb
if code no exists, the memory's size is 0.4Mb
closed account (48bpfSEw)
Compilers have a lot of options. If you don't like this behaviour switch of the code-optimization!
Topic archived. No new replies allowed.