Results 1 to 12 of 12

Thread: [RESOLVED] CopyMemory works in IDE / Not Compiled

  1. #1

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,528

    Resolved [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.



  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,225

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,528

    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.



  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,225

    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?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,528

    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.



  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,225

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    Addicted Member
    Join Date
    Mar 2019
    Posts
    137

    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.

  8. #8
    Frenzied Member
    Join Date
    Dec 2014
    Posts
    1,070

    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.

  9. #9

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,528

    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.



  10. #10
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,225

    Re: CopyMemory works in IDE / Not Compiled

    Quote Originally Posted by some1uk03 View Post
    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?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  11. #11

    Thread Starter
    Frenzied Member some1uk03's Avatar
    Join Date
    Jun 2006
    Location
    London, UK
    Posts
    1,528

    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.



  12. #12
    PowerPoster Arnoutdv's Avatar
    Join Date
    Oct 2013
    Posts
    3,858

    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
  •  



Click Here to Expand Forum to Full Width