-
Nov 14th, 2019, 11:05 AM
#1
Thread Starter
Frenzied Member
[RESOLVED] CopyMemory works in IDE / Not Compiled
Facing an interesting dilemma here.
What works perfectly fine in IDE, is causing a CRASH once compiled.
I've tracked it down to it being an issue with CopyMemory.
Any ideas or reasons, why it seems fine in IDE and not compiled?
Code:
ReDim wavData(i - startPos)
Call CopyMemory(wavData(0), origBuff(startPos), (UBound(wavData) * 2) + 1)
Last edited by some1uk03; Nov 14th, 2019 at 11:09 AM.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Nov 14th, 2019, 11:37 AM
#2
Re: CopyMemory works in IDE / Not Compiled
This almost always is the case of copying more bytes than exist or the source/destination addresses are invalid.
Assumption: Your CopyMemory API declaration has 1st two parameters defined as "By Ref" vs "By Val"
wavData(0), don't see a problem there assuming wavData was declared elsewhere as at least a 2-byte vartype: Integer, Long, etc
origBuff(startPos), don't see a problem there either
UBound(wavData) * 2) + 1 must not be greater than (UBound(wavData)+1) in bytes
But to be absolutely sure, can you provide your CopyMemory API declaration along with how you declared wavData & origBuffer?
Last edited by LaVolpe; Nov 14th, 2019 at 11:47 AM.
-
Nov 14th, 2019, 11:55 AM
#3
Thread Starter
Frenzied Member
Re: CopyMemory works in IDE / Not Compiled
Code:
Private Declare Sub CopyMemory Lib "kernel32" Alias "RtlMoveMemory" (pDst As Any, pSrc As Any, ByVal ByteLen As Long)
Private wavData() As Integer
Private origBuff() As Integer
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Nov 14th, 2019, 12:22 PM
#4
Re: CopyMemory works in IDE / Not Compiled
The problem is likely this: number of bytes being copied exceed total number of bytes remaining from: origBuff(startPos)
Total number of bytes remaining from that starting point would be: (UBound(origBuff) - startPos) * 2 + 2
so, to test over calculating, add this before the CopyMemory line:
Code:
Debug.Assert (UBound(wavData) * 2) + 1) <= (UBound(origBuff) - startPos) * 2 + 2
Hopefully, I did my math correctly. If so, your code in IDE should stop on that line.
Note: I think the +1 in your code should be +2 and you changed it to +1 due to the crashing?
-
Nov 14th, 2019, 12:59 PM
#5
Thread Starter
Frenzied Member
Re: CopyMemory works in IDE / Not Compiled
Ok, that does seem to stop during IDE.
However, when I debug the math, it doesn't seem to be overflowing.
Code:
StartPos chunkSize
----------------------------------
0 29704
29705 30473
60179 19611
79791 25587
105379 24359
129739 31293
161033 18735
179769 26195
205965 23635
229601 30329
259931 19631
279563 25537
305101 24451
329553 29605
359159 20259
379419 25569
404989 24353
429343 30321
459665 19601
479267 24939
504207 25001
529209 30311
559521 20359
579881 24829
604711 24353
629065 30775
659841 19173
679015 25953
704969 24505
729475 18905
748381 11559
759941 13403
773345 25449
------------------------------------
773345 + 25449 = 798794
Also another factor to note is that, the crash when compiled, does NOT happen at the last chunk/loop. (hence why i don't think it's an overflow issue)
Happens after the 1st split.
All I'm trying to do is, split a big chunk into smaller chunk arrays.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Nov 14th, 2019, 03:17 PM
#6
Re: CopyMemory works in IDE / Not Compiled
I'd suggest appending to a log, just before every CopyMemory call, information relative to the crash. Once the crash occurs, the last item in the log is the one that caused the crash.
At a minimum, include: UBound(wavData), UBound(origBuff), and startPos
Another thing and you probably already ensured this... You don't mention how the variables i and startPos are calculated. Remember than wavData(i - StartPos) calculates index for an integer array and must not be some byte offset calculation. Each array index = 2 bytes.
-
Nov 15th, 2019, 01:29 AM
#7
Hyperactive Member
Re: CopyMemory works in IDE / Not Compiled
I have overcome these type of issues before which were always related to my incorrect calculation of output buffer size/location by adding a set of "guard bytes" at the end of my structure and pre populating them with something before executing copy memory. In this way you can know almost for sure if you are overwriting something. Probably it works in the IDE because you are trashing something that either does not matter or has not been touched by the IDE code yet and eventually will cause a crash when it does.
-
Nov 15th, 2019, 09:56 AM
#8
Re: CopyMemory works in IDE / Not Compiled
another problem I get from API's is,
when using a typelib, and depending on computer (so it can work on one computer but not another) is that in one it can be unicode but in another its ansi.
now I always add the API in the module to avoid this problem.
-
Nov 15th, 2019, 01:36 PM
#9
Thread Starter
Frenzied Member
Re: CopyMemory works in IDE / Not Compiled
I've been going over this for some time now and I'm starting to think is this a VB bug or what?
Everything seems ok. Even at the 1st copyMemory it crashes (on compile).
So, overbuffering is out of question.
I made my buffer, double size, (just to make sure there is enough space in the buffer) and same issue.
Still doesn't make sense when IDE doesn't crash.
What alternative API's are there to replace copyMemory?
Last edited by some1uk03; Nov 15th, 2019 at 01:39 PM.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Nov 15th, 2019, 02:15 PM
#10
Re: CopyMemory works in IDE / Not Compiled
Originally Posted by some1uk03
What alternative API's are there to replace copyMemory?
It isn't CopyMemory that is the problem. I'd suspect a high percentage of running processes use that API. When seen uncompiled, it is a fairly simple function.
A simple alternative is to use a for:next loop, especially if you are copying between arrays of the same data type. Maybe in doing so, you might discover an error in calculating your variables: i and startPos?
-
Nov 15th, 2019, 04:55 PM
#11
Thread Starter
Frenzied Member
Re: CopyMemory works in IDE / Not Compiled
I seem to have found the issue at last!!!
Not sure if it's a copyMemory API issue after all or a real VB bug, but.....
What seemed to have been causing the issue is, after the copyMemory line, the buffer was being converted and passed down to another function.
Code:
ReDim wavData(i - startPos)
Call CopyMemory(wavData(0), origBuff(startPos), (UBound(wavData) * 2))
Call LoadToMainBuff(convertIntegerToByte(wavData)) '///CRASH <<<<<<<<<<
FIX
Code:
ReDim wavData(i - startPos)
Call CopyMemory(wavData(0), origBuff(startPos), (UBound(wavData) * 2))
Dim tmpByteArray() as Byte
tmpByteArray = convertIntegerToByte(wavData)
Call LoadToMainBuff(tmpByteArray)
I have a suspicion that since copyMemory values weren't passed down as ByVal, the original Buff might somehow be still in use in memory cos it's being referenced. And on next line, as it's being used again, it was causing the crash?
Well, it seems, using a new variable (tmpByteArray) with the result and passing that down, fixed the issue.
_____________________________________________________________________
----If this post has helped you. Please take time to Rate it.
----If you've solved your problem, then please mark it as RESOLVED from Thread Tools.
-
Nov 18th, 2019, 05:43 AM
#12
Re: [RESOLVED] CopyMemory works in IDE / Not Compiled
What does "convertIntegerToByte" do?
Can you share the source?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|