【100】关于GlobalAlloc、GlobalReAlloc、GlobalFree

zyrr159487 2016-09-30 11:03:43
很老的一份算法代码,大概是92年的元老写的。其中关于内存处理这一块用到了以下函数。使用GlobalReAlloc、GlobalFree来控制和释放内存。
目前在使用过程中遇到内存没有释放的问题,对方建议:目前硬件资源很充足,没必要再使用这种方式,建议使用其他方式替换。
请问,应该使用哪种方式替换比较方便,这份源码DLL项目中。

char * ATIAlloc(DWORD size)
{
long i, index;
PtrRecord *ptrs;
HANDLE hdl;
char *tmpPtr = NULL;

if (PtrHandle) {
ptrs = (PtrRecord *)GlobalLock(PtrHandle);
index = -1;
for (i = 0; i < maxPtrs; i++) {
if (ptrs[i].emptyFlag) {
index = i;
break;
}
}
if (index < 0) {
GlobalUnlock(PtrHandle);
hdl = GlobalReAlloc(PtrHandle, (DWORD)((DWORD)sizeof(PtrRecord) * (DWORD)(maxPtrs + PTR_BLOCK)), GMEM_MOVEABLE | GMEM_ZEROINIT);
if (!hdl) {
return NULL;
}
PtrHandle = hdl;
ptrs = (PtrRecord *)GlobalLock(PtrHandle);
for (i = maxPtrs; i < maxPtrs + PTR_BLOCK; i++) {
ptrs[i].hdl = NULL;
ptrs[i].ptr = NULL;
ptrs[i].emptyFlag = TRUE;
}
index = maxPtrs;
maxPtrs += PTR_BLOCK;
}
_ALLOC(hdl, size);
if (hdl) {
ptrs[index].hdl = hdl;
ptrs[index].ptr = (char *)GlobalLock(hdl);
ptrs[index].emptyFlag = FALSE;
tmpPtr = ptrs[index].ptr;
}
GlobalUnlock(PtrHandle);
return (hdl) ? /*ptrs[index].ptr*/tmpPtr: NULL;
}
else {
return NULL;
}
}



char * ATIRealloc(char *ptr, DWORD size)
{
long i, index;
PtrRecord *ptrs;
HANDLE hdl;
char *tmpPtr = NULL;

HeapReAlloc()
GlobalReAlloc()
if (PtrHandle) {
ptrs = (PtrRecord *)GlobalLock(PtrHandle);
index = -1;
for (i = 0; i < maxPtrs; i++) {
if (!(ptrs[i].emptyFlag)) {
if (ptr == ptrs[i].ptr) {
index = i;
break;
}
}
}
if (index >= 0) {
hdl = ptrs[index].hdl;
GlobalUnlock(hdl);
hdl = GlobalReAlloc(hdl, (DWORD)size, GMEM_MOVEABLE | GMEM_ZEROINIT);
if (hdl) {
ptrs[index].hdl = hdl;
ptrs[index].ptr = (char *)GlobalLock(hdl);
tmpPtr = ptrs[index].ptr;
}
}
else {
hdl = NULL;
}
GlobalUnlock(PtrHandle);
return (hdl) ? tmpPtr/*ptrs[index].ptr*/: NULL;
}
else {
return NULL;
}
}



void ATIFree(char *ptr)
{
long i, index;
PtrRecord *ptrs;
HANDLE hdl;

if (PtrHandle) {
ptrs = (PtrRecord *)GlobalLock(PtrHandle);
index = -1;
for (i = 0; i < maxPtrs; i++) {
if (!(ptrs[i].emptyFlag)) {
if (ptr == ptrs[i].ptr) {
index = i;
break;
}
}
}
if (index >= 0) {
hdl = ptrs[index].hdl;
GlobalUnlock(hdl);
GlobalFree(hdl);
ptrs[index].hdl = NULL;
ptrs[index].ptr = NULL;
ptrs[index].emptyFlag = TRUE;
}
GlobalUnlock(PtrHandle);
}
}



BOOL ATICopy(char *to, char *from, DWORD size)
{
DWORD i;

if (to && from) {
for (i = 0; i < size; i++) {
to[i] = from[i];
}
return TRUE;
}
else {
return FALSE;
}
}
...全文
484 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
赵4老师 2016-09-30
  • 打赏
  • 举报
回复
《Windows核心编程》
  • 打赏
  • 举报
回复
如果运行正常,没内存泄漏,最好不要改了。
pengzhixi 2016-09-30
  • 打赏
  • 举报
回复
直接malloc,free应该就可以了。只是再编译链接的时候注意编译参数就是了
paschen 版主 2016-09-30
  • 打赏
  • 举报
回复
用 malloc realloc free 或者 new delete
ztenv 版主 2016-09-30
  • 打赏
  • 举报
回复
直接new/delete,如果考虑效率的话就配合内存池;
Creating Windows CreateMDIWindow CreateWindow CreateWindowEx RegisterClass RegisterClassEx UnregisterClass Message Processing BroadcastSystemMessage CallNextHookEx CallWindowProc DefFrameProc DefMDIChildProc DefWindowProc DispatchMessage GetMessage GetMessageExtraInfo GetMessagePos GetMessageTime GetQueueStatus InSendMessage PeekMessage PostMessage PostQuitMessage PostThreadMessage RegisterWindowMessage ReplyMessage SendMessage SendMessageCallback SendMessageTimeout SendNotifyMessage SetMessageExtraInfo SetWindowsHookEx TranslateMessage UnhookWindowsHookEx WaitMessage Window Information AnyPopup ChildWindowFromPoint ChildWindowFromPointEx EnableWindow EnumChildWindows EnumPropsEx EnumThreadWindows EnumWindows FindWindow FindWindowEx GetClassInfoEx GetClassLong GetClassName GetClientRect GetDesktopWindow GetFocus GetForegroundWindow GetNextWindow GetParent GetProp GetTopWindow GetWindow GetWindowLong GetWindowRect GetWindowText GetWindowTextLength IsChild IsIconic IsWindow IsWindowEnabled IsWindowUnicode IsWindowVisible IsZoomed RemoveProp SetActiveWindow SetClassLong SetFocus SetForegroundWindow SetParent SetProp SetWindowLong SetWindowText WindowFromPoint Processes and Threads CreateEvent CreateMutex CreateProcess CreateSemaphore CreateThread DeleteCriticalSection DuplicateHandle EnterCriticalSection ExitProcess ExitThread GetCurrentProcess GetCurrentProcessId GetCurrentThread GetCurrentThreadId GetExitCodeProcess GetExitCodeThread GetPriorityClass GetThreadPriority GetWindowThreadProcessId InitializeCriticalSection InterlockedDecrement InterlockedExchange InterlockedIncrement LeaveCriticalSection OpenEvent OpenMutex OpenProcess OpenSemaphore PulseEvent ReleaseMutex ReleaseSemaphore ResetEvent ResumeThread SetEvent SetPr

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧