神奇的LocalAlloc

一同事遇到一个问题,调用一个dll传入写死的字符串ok,传入动态创建的字符串返回错误。折腾来折腾去,最后使用LocalAlloc解决了问题。但 MSDN也写着函数只是为兼容16位版本而保留的,并没有什么实际作用。。。费解!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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; } 优化这段代码,释放一下内存,内存一直报错
最新发布
05-15
这段代码中确实有一些内存泄漏的问题,主要是没有释放 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 等,来帮助定位问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值