int main() { std::map<std::string, std::string> config = readIniFile("config.ini"); int bluetoothCount = std::stoi(config["bluetooth.count"]); std::string logFileName = config["log.filename"]; int searchTime = std::stoi(config["bluetooth.searchtime"]); // 添加搜索时间配置项 WSADATA wsaData; int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData); if (iResult != 0) { std::cout << "WSAStartup failed: " << iResult << std::endl; return 1; } WSAQUERYSET service; memset(&service, 0, sizeof(service)); service.dwSize = sizeof(service); service.dwNameSpace = NS_BTH; HANDLE lookupHandle = NULL; iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle); if (iResult != 0) { std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl; WSACleanup(); return 1; } int count = 0; WSAQUERYSET* pResult = (WSAQUERYSET*)LocalAlloc(LPTR, sizeof(WSAQUERYSET)); int searchCount = 0; // 添加搜索计数器 while (searchCount < searchTime) { // 添加计时器 DWORD dwSize = sizeof(WSAQUERYSET); iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pResult); if (iResult != 0) { break; } count++; searchCount++; // 每次循环增加已搜索时间 Sleep(100000); // 等待1秒,再进行下一次搜索 if (searchCount >= searchTime) { // 如果已经搜索了设定的时间,跳出循环 break; } } LocalFree(pResult); WSALookupServiceEnd(lookupHandle); WSACleanup(); if (count >= bluetoothCount) { std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } else { std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount); writeLogFile(logFileName, logText); } return 0; } 优化这段代码,释放一下内存,内存一直报错
时间: 2023-06-17 16:07:39 浏览: 214
这段代码中确实有一些内存泄漏的问题,主要是没有释放 LocalAlloc 分配的内存,建议在使用完毕后,使用 LocalFree 函数释放内存。以下是优化后的代码:
```
int main() {
std::map<std::string, std::string> config = readIniFile("config.ini");
int bluetoothCount = std::stoi(config["bluetooth.count"]);
std::string logFileName = config["log.filename"];
int searchTime = std::stoi(config["bluetooth.searchtime"]);
// 添加搜索时间配置项
WSADATA wsaData;
int iResult = WSAStartup(MAKEWORD(2, 2), &wsaData);
if (iResult != 0) {
std::cout << "WSAStartup failed: " << iResult << std::endl;
return 1;
}
WSAQUERYSET service;
memset(&service, 0, sizeof(service));
service.dwSize = sizeof(service);
service.dwNameSpace = NS_BTH;
HANDLE lookupHandle = NULL;
iResult = WSALookupServiceBegin(&service, LUP_CONTAINERS, &lookupHandle);
if (iResult != 0) {
std::cout << "WSALookupServiceBegin failed: " << iResult << std::endl;
WSACleanup();
return 1;
}
int count = 0;
WSAQUERYSET* pResult = (WSAQUERYSET*)LocalAlloc(LPTR, sizeof(WSAQUERYSET));
int searchCount = 0; // 添加搜索计数器
while (searchCount < searchTime) {
// 添加计时器
DWORD dwSize = sizeof(WSAQUERYSET);
iResult = WSALookupServiceNext(lookupHandle, LUP_RETURN_NAME | LUP_RETURN_ADDR, &dwSize, pResult);
if (iResult != 0) {
break;
}
count++;
searchCount++; // 每次循环增加已搜索时间
Sleep(100000); // 等待1秒,再进行下一次搜索
if (searchCount >= searchTime) { // 如果已经搜索了设定的时间,跳出循环
break;
}
}
LocalFree(pResult); // 释放分配的内存
WSALookupServiceEnd(lookupHandle);
WSACleanup();
if (count >= bluetoothCount) {
std::string logText = "Bluetooth count is " + std::to_string(count) + ", reached the target count of " + std::to_string(bluetoothCount);
writeLogFile(logFileName, logText);
} else {
std::string logText = "Bluetooth count is " + std::to_string(count) + ", did not reach the target count of " + std::to_string(bluetoothCount);
writeLogFile(logFileName, logText);
}
return 0;
}
```
另外,如果内存泄漏问题仍然存在,可以使用内存泄漏检测工具,例如 Visual Leak Detector 等,来帮助定位问题。
阅读全文