WinAsm Studio, The Assembly IDE - Free Downloads, Source Code
Sponsors
Articles
Programming Quick Start
32-bit Assembler is Easy
Porting Iczelion tutorials
What is FASM
Hard Drive Recovery
Wiring your own LAN
Personal menu
Welcome Guest
User:
Pass:
Register!
Resend Validation Email
 
Forum
Pages (2) [1] 2   ( Go to first unread post )

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

 
What is your opinon on the szRepGetBSize Procedure
Perfect! [ 0 ]  [0.00%]
Could be better! [ 1 ]  [50.00%]
Useless! [ 1 ]  [50.00%]
Total Votes: 2
Guests cannot vote 
AvidStudent
Quote Post


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

PMEmail PosterUsers WebsiteYahoo
Top
ragdog
Quote Post


Extremely Active Member
******

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


Hi Avid

Can your upload the complete source?
PMUsers Website
Top
shoorick
Quote Post


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 smile.gif 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.
PMEmail PosterUsers Website
Top
ragdog
Quote Post


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
PMUsers Website
Top
AvidStudent
Quote Post


Extremely Active Member
******

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


Hey ragdog, I was gonna upload the source but I found out I had a huge debug listing in the files so I deleted it and recompiled everything, then the whole app was broken. I am guessing this had something to do with the errors I was getting.

I am rewriting the FindAndReplace proc line by line to see where the error rises. Hopefully I get it taken care of. if I don't I'll post the files here later.
PMEmail PosterUsers WebsiteYahoo
Top
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

Topic Options Pages (2) [1] 2  Reply to this topicStart new topicStart Poll

 

Sponsors
Computer Science

Internet
C/C++
Hardware & PC maintenance

HiEditor

General Discussions
Suggestions/Bug Reports
WinAsm Studio

General Discussions
Suggestions/Bug Reports
WinAsm Studio FAQ
Multilingual User Interface
Add-Ins
Assembly Programming

Main
Newbies
Projects
Custom Controls
Snippets
Announcements & Rules

Announcements

General

Online Degrees - Distance Learning
The Heap
Russian
Google