|
Forum
|
|
|
Memory write error, exported procedure has memory write error if the input buffer is larger than aound 20 or more kilobytes
|
|
| AvidStudent |
|

Extremely Active Member
     
Group: Members
Posts: 234
Member No.: 17238
Joined: 10-January 09

|
I was writing a find and replace procedure to be exported in a DLL project. The memory write error was caused by an invalid buffer size. I had allocated space for a string buffer without adding some padding to the end as well as space for the zero terminator. This caused a page fault that was reported as a memory write error. When you get this type of error I suggest that you CHECK YOUR BUFFERS FOR PROPER SIZE and read Shoorick's comments below!
On another note, how am I supposed to free a local buffer that must be passed to another program? When the buffer is freed the data no longer exists. Perhaps the program receiving the data should free it?
Here is the updated and working code.
| CODE | FindAndReplace proc lpSzFind:LPSTR, lpSzReplace:LPSTR, lpSzText:LPSTR
LOCAL szBuffer:BYTE LOCAL lpBuffer:DWORD LOCAL dwBufferSize:DWORD mov eax, lpSzFind cmp eax, 0 jz no_handle mov eax, lpSzReplace cmp eax, 0 jz no_handle mov eax, lpSzText cmp eax, 0 jz no_handle lea eax, szBuffer mov lpBuffer, eax push lpSzText push lpSzReplace push lpSzFind call szRepGetBSize mov dwBufferSize, eax
mov lpBuffer, alloc(dwBufferSize) cmp eax, 0 jz no_memory invoke szRep, lpSzText, lpBuffer, lpSzFind, lpSzReplace ;STDCALL VERSION mov eax, lpBuffer ;Visual Basic 6 Version using AvidStudent's BSTR.inc ;push lpBuffer ;call BSTR ret 12 no_handle:
ret 12 no_memory: ;<?> How can we raise an error in this event <?> ret 12
FindAndReplace endp
;Calling PROC must check for valid handles to increase code performance. szRepGetBSize proc lpSzFind:DWORD, lpSzReplace:DWORD, lpSzText:DWORD LOCAL BSIZE:DWORD LOCAL IWRDS:DWORD push lpSzText call StrLen ;This line did not exist and was the source of memory error add eax, 5 ;4 BYTES Padding + NULL CHAR mov BSIZE, eax push lpSzFind call StrLen mov ecx, eax push lpSzFind push lpSzText call szWcnt mov IWRDS, eax
mul ecx sub BSIZE, eax push lpSzReplace call StrLen mov ecx, eax mov eax, IWRDS mul ecx add BSIZE, eax mov eax, BSIZE
ret 12
szRepGetBSize endp
|
If you are going to add this to your project make sure you take note of shoorick's comments below. As well as ragdog's code posting
|
| |
|
|
|
| shoorick |
|

Extremely Active Member
     
Group: Admins
Posts: 2301
Member No.: 160
Joined: 22-June 04

|
it is necesary to study subject starting from memory allocation methods. i do not know how "alloc()" works exactly, but there are some prefered methods with winapi: HeapAlloc and VirtualAlloc. HeapAlloc will let you allocate any size of memory block on NT systems, but recomended below 4Mb (look msdn better), while VirtualAlloc lets you allocate huge memory areas, but in anyway you will not be able to allocate 2048 Mb with this way. there also one method exists with pages which lets allocation of huge memory blocks, but i even do not remember names of these functions  on stack you may easy allocate memory block with fast access, but process usually gets 4Mb stack, and also has other data, so, not of all 4Mb are available to allocate without stack overflow. finally, such operations, like you suggest, better provide via streams: load part, process, save, then load next part - it will be faster, less resource consumping and not too more complex then plain memory addressing.
|
| |
|
|
|
| ragdog |
|

Extremely Active Member
     
Group: Moderators
Posts: 873
Member No.: 5019
Joined: 13-May 07

|
For Alloc memory use i this
| CODE | Local @lpBuffer:DWORD Local @hSize :DWORD invoke lstrLen,addr lpszText mov @hSize,eax invoke VirtualAlloc,0,eax,MEM_COMMIT,PAGE_EXECUTE_READWRITE test eax,eax jz @Error mov @lpBuffer, eax ... .. .
invoke VirtualFree,@lpBuffer,hSize,MEM_RELEASE @Error:
|
I´m not sure how fast it is you can make a speed test For my works is this ok
|
| |
|
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:
Track this topic
Receive email notification when a reply has been made to this topic and you are not active on the board.
Subscribe to this forum
Receive email notification when a new topic is posted in this forum and you are not active on the board.
Download / Print this Topic
Download this topic in different formats or view a printer friendly version.
|
|
|