在VC6.0的MFC appwizard环境中,调用LocalAlloc()函数时,运行时总通不过,调试时报错如下:
"0x10218253"指令引用的"0x00000000"内存.该内存不能为"read".
本人百思不得其解,请高手指教!

解决方案 »

  1.   

    replace LocalAlloc with GolbalAlloc
      

  2.   

    代码如下,请各位高手不吝指教,本人不胜感激!!!!
    //声明变量
    int nSize;
    LPBYTE lpBuf1, lpBuf2, lpBufOut;
    HFILE hFile1, hFile2, hFileOut;
    LPBITMAPINFO lpInfo;
    HDC hDC;
    int i;

    //打开文件(bmp格式的图片)
    hFile1 = _lopen("..//BMP//1.bmp",OF_READ);
    hFile2 = _lopen("..//BMP//2.bmp",OF_READ);
    hFileOut =_lcreat("..//BMP//out.bmp",0); //添加一个新文件

    nSize = GetFileSize((HANDLE) hFile1, NULL); //读取hFile1(bmp图片)文件的大小
    lpBuf1 = LocalAlloc(LPTR, nSize); //申请nSize大小的文件,就是在此处出错,申请失败,不知是何原因?
    lpBuf2 = LocalAlloc(LPTR, nSize);
    lpBufOut = LocalAlloc(LPTR, nSize);

    _lread(hFile1, lpBuf1, nSize);
    _lread(hFile2, lpBuf2, nSize);

    _lclose(hFile1);
    _lclose(hFile2);

    memcpy(lpBufOut, lpBuf1, nSize);
      

  3.   

    用GolbalAlloc替换LocalAlloc试一下呢?
      

  4.   

    Note  The local functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. 所以用HeapAlloc替换LocalAlloc试试
      

  5.   

    建议仔细阅读MSDN中有关内存分配的内容,对各种内存分配有详述
      

  6.   


    Platform SDK: Memory Management 
    Memory Management FunctionsThe following functions are used in memory management.Function Description 
    CopyMemory Copies a block of memory from one location to another. 
    CreateMemoryResourceNotification Creates a memory resource notification object. 
    FillMemory Fills a block of memory with a specified value. 
    GetLargePageMinimum Retrieves the minimum size of a large page. 
    GetWriteWatch Retrieves the addresses of the pages that have been written to in a region of virtual memory. 
    GlobalMemoryStatus Obtains information about the system's current usage of both physical and virtual memory. 
    GlobalMemoryStatusEx Obtains information about the system's current usage of both physical and virtual memory. 
    IsBadCodePtr Determines whether the calling process has read access to the memory at the specified address. 
    IsBadReadPtr Verifies that the calling process has read access to the specified range of memory. 
    IsBadStringPtr Verifies that the calling process has read access to a range of memory pointed to by a string pointer. 
    IsBadWritePtr Verifies that the calling process has write access to the specified range of memory. 
    MoveMemory Moves a block of memory from one location to another. 
    QueryMemoryResourceNotification Retrieves the state of the specified memory resource object. 
    ResetWriteWatch Resets the write-tracking state for a region of virtual memory. 
    SecureZeroMemory Fills a block of memory with zeros. 
    ZeroMemory Fills a block of memory with zeros. 
    The following are the AWE functions.Function Description 
    AllocateUserPhysicalPages Allocates physical memory pages to be mapped and unmapped within any AWE region of the process. 
    FreeUserPhysicalPages Frees physical memory pages previously allocated with AllocateUserPhysicalPages. 
    MapUserPhysicalPages Maps previously allocated physical memory pages at the specified address within an AWE region. 
    MapUserPhysicalPagesScatter Maps previously allocated physical memory pages at the specified address within an AWE region. The following are the global and local functions. These functions are slower than other memory management functions and do not provide as many features. Therefore, new applications should use the heap functions. However, the global functions are still used with DDE and the clipboard functions.Function Description 
    GlobalAlloc Allocates the specified number of bytes from the heap. 
    GlobalDiscard Discards the specified global memory block. 
    GlobalFlags Returns information about the specified global memory object. 
    GlobalFree Frees the specified global memory object. 
    GlobalHandle Retrieves the handle associated with the specified pointer to a global memory block. 
    GlobalLock Locks a global memory object and returns a pointer to the first byte of the object's memory block. 
    GlobalReAlloc Changes the size or attributes of a specified global memory object. 
    GlobalSize Retrieves the current size of the specified global memory object. 
    GlobalUnlock Decrements the lock count associated with a memory object. 
    LocalAlloc Allocates the specified number of bytes from the heap. 
    LocalDiscard Discards the specified local memory object. 
    LocalFlags Returns information about the specified local memory object. 
    LocalFree Frees the specified local memory object. 
    LocalHandle Retrieves the handle associated with the specified pointer to a local memory object. 
    LocalLock Locks a local memory object and returns a pointer to the first byte of the object's memory block. 
    LocalReAlloc Changes the size or the attributes of a specified local memory object. 
    LocalSize Returns the current size of the specified local memory object. 
    LocalUnlock Decrements the lock count associated with a memory object. 
    The following are the heap functions.Function Description 
    GetProcessHeap Obtains a handle to the heap of the calling process. 
    GetProcessHeaps Obtains handles to all of the heaps that are valid for the calling process. 
    HeapAlloc Allocates a block of memory from a heap. 
    HeapCompact Attempts to compact a specified heap. 
    HeapCreate Creates a heap object. 
    HeapDestroy Destroys the specified heap object. 
    HeapFree Frees a memory block allocated from a heap. 
    HeapLock Attempts to acquire the lock associated with a specified heap. 
    HeapQueryInformation Retrieves information about the specified heap. 
    HeapReAlloc Reallocates a block of memory from a heap. 
    HeapSetInformation Sets heap information for the specified heap. 
    HeapSize Retrieves the size of a memory block allocated from a heap. 
    HeapUnlock Releases ownership of the lock associated with a specified heap. 
    HeapValidate Attempts to validate a specified heap. 
    HeapWalk Enumerates the memory blocks in a specified heap. The following are the virtual memory functions.Function Description 
    VirtualAlloc Reserves or commits a region of pages in the virtual address space of the calling process. 
    VirtualAllocEx Reserves or commits a region of pages in the virtual address space of the specified process. 
    VirtualFree Releases or decommits a region of pages within the virtual address space of the calling process. 
    VirtualFreeEx Releases or decommits a region of memory within the virtual address space of a specified process. 
    VirtualLock Locks the specified region of the process's virtual address space into physical memory. 
    VirtualProtect Changes the access protection on a region of committed pages in the virtual address space of the calling process. 
    VirtualProtectEx Changes the access protection on a region of committed pages in the virtual address space of the calling process. 
    VirtualQuery Provides information about a range of pages in the virtual address space of the calling process. 
    VirtualQueryEx Provides information about a range of pages in the virtual address space of the calling process. 
    VirtualUnlock Unlocks a specified range of pages in the virtual address space of a process. 
    Platform SDK Release: February 2003  What did you think of this topic?
      Order a Platform SDK CD  
      

  7.   


    没用过
    我都用GloablAlloc和GlobalReAlloc
      

  8.   

    GolbalAlloc 和 LocalAlloc 都不提倡使用
    他们的速度都比较慢,而其功能较少
    你看看MSDN吧
    微软建议用heap functions
      

  9.   

    lpBuf1 = (LPBYTE)LocalAlloc(LPTR, nSize);