首页 > 解决方案 > 如何正确使用 LocalAlloc 和 LocalReAlloc

问题描述

我将学习如何使用LocalAlloc以及LocalReAllocWin32 API。我编写了以下代码,但它给了我例外。我不知道下面的代码有什么问题。

#include <Windows.h>
#include <iostream>

namespace code
{
    namespace memory
    {
        void allocation()
        {
            char* string = reinterpret_cast<char*>(LocalAlloc(LPTR, 6 + 1));
            CopyMemory(string, "WINAPI", 6);
            std::printf("%s\n", string);


            string = reinterpret_cast<char*>(LocalReAlloc(string, 6 + 13 + 1, LMEM_MOVEABLE));
            CopyMemory(string + 6, "IS THE BEST", 13);
            std::printf("%s\n", string);

            delete string;
        }
    }
}
int main(int argc, char* argv[])
{
    code::memory::allocation();

    return 0;
}

当我编译上面的程序时,它没有给我任何错误,但是当我运行它时,它给了我异常。以下消息来自异常:

---------------------------
Microsoft Visual C++ Runtime Library
---------------------------
Debug Assertion Failed!

Program: ...Windows\00 Windows API Programming\Debug\52_DynamicMemory.exe
File: minkernel\crts\ucrt\src\appcrt\heap\debug_heap.cpp
Line: 904

Expression: _CrtIsValidHeapPointer(block)

For information on how your program can cause an assertion
failure, see the Visual C++ documentation on asserts.

(Press Retry to debug the application)

---------------------------
Abort   Retry   Ignore   
---------------------------

标签: c++winapi

解决方案


您的代码有几个问题。

完全缺乏错误处理。

如果LocalReAlloc()失败,您将泄漏由LocalAlloc().

第二个CopyMemory()是超出了被复制的字符串文字的范围。printf()如果文字被正确复制,您并不能确保为以下内容重新分配的内存为空终止。

您没有正确释放分配的内存。你必须使用LocalFree(),而不是delete

试试这个:

#include <Windows.h>
#include <cstdio>
#include <cstring>

namespace code
{
    namespace memory
    {
        void allocation()
        {
            char* string = static_cast<char*>(LocalAlloc(LMEM_FIXED, 6 + 1));
            if (!string) return;

            std::strcpy(string, "WINAPI");
            std::printf("%s\n", string);

            char* newstring = static_cast<char*>(LocalReAlloc(string, 6 + 12 + 1, LMEM_MOVABLE));
            if (!newstring) { LocalFree(string); return; }
            string = newstring;

            std::strcpy(string + 6, " IS THE BEST");
            std::printf("%s\n", string);

            LocalFree(string);
        }
    }
}

int main()
{
    code::memory::allocation();

    return 0;
}

推荐阅读