Results 1 to 16 of 16

Thread: [RESOLVED] VB6 project not working "quite" right in tB (can't find the root cause)

  1. #1

    Thread Starter
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,332

    Resolved [RESOLVED] VB6 project not working "quite" right in tB (can't find the root cause)

    I have made an exact tB copy of my VB6 Image Metadata project posted in the VB6 Codebank Forum. I did not use the import feature but rather copied all module code manually and recreated the form design to see if I could match it as closely as possible.

    While the project runs flawlessly in VB6 all the time, in tB it only runs "sometimes" (for lack of a better word) and when it runs, the list of metadata properties is incomplete. The other times it just crashes with access violation. Not even going to mention that the image is incorrectly displayed upside down (this doesn't happen in VB6), since this can be categorized as an overlook rather than a bug!

    Name:  MetaDataProperties_tB.jpg
Views: 715
Size:  68.5 KB

    The fact that the list of metadata properties is incomplete (compared to the VB6 version) leads me to believe that not all async operations make it to the finish line and there may be some sort of "race condition" happening in the tB version. In VB6 all async operations are completed all the time, even if not always in the same order but they all finish without exceptions and without crashes.

    I'd appreciate it if Wayne or some else more knowledgeable than me could take a look at the project:

    MetaDataProperties.zip - contains both VB6 and tB versions (without the test image shown above since that exceeds the forum attachment size limit!)

    * Later Edit: here's the link for the test image.

  2. #2
    PowerPoster yereverluvinuncleber's Avatar
    Join Date
    Feb 2014
    Location
    Norfolk UK (inbred)
    Posts
    2,965

    Re: VB6 project not working "quite" right in tB (can't find the root cause)

    I've had a problem in one of my apps where TB, when reading an image from a RC collection, the image was inverted. I raised a github issue for it.

    It may be entirely unrelated but the issue is here: https://github.com/twinbasic/twinbasic/issues/1939
    Last edited by yereverluvinuncleber; Mar 10th, 2025 at 07:25 AM.
    https://github.com/yereverluvinunclebert

    Skillset: VMS,DOS,Windows Sysadmin from 1985, fault-tolerance, VaxCluster, Alpha,Sparc. DCL,QB,VBDOS- VB6,.NET, PHP,NODE.JS, Graphic Design, Project Manager, CMS, Quad Electronics. classic cars & m'bikes. Artist in water & oils. Historian.

    By the power invested in me, all the threads I start are battle free zones - no arguing about the benefits of VB6 over .NET here please. Happiness must reign.

  3. #3

    Thread Starter
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,332

    Re: VB6 project not working "quite" right in tB (can't find the root cause)

    The problem with the inverted image is probably related to the BITMAPINFOHEADER structure where the general consensus is the biHeight member should be negative so that the image is scanned top to bottom instead of bottom to top. So the solution is likely to be as simple as correcting the sign.

    The other issue is a lot more complicated. Most WinRT methods are asynchronous in nature so they are running in their own thread and then they call back into the main thread to communicate their results. The order in which these async operations finish is non-deterministic. I am amazed how VB6 with its 30 years old COM technology is able to properly "serialize" or "queue" these callbacks so that they don't step on each other's toes. I think this has some merit to be looked into.

  4. #4
    Junior Member
    Join Date
    Jun 2015
    Location
    Netherlands
    Posts
    27

    Re: VB6 project not working "quite" right in tB (can't find the root cause)

    Problems like these are the reason I decided not to switch to tB. A complex and rare bug in the compiler can ruin your entire project. The main thing in a compiler is reliability and predictability, for this reason I stayed with VB6.

  5. #5
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    6,955

    Re: VB6 project not working "quite" right in tB (can't find the root cause)

    When nothing is more important, yeah it might make sense to avoid projects in Beta. Beta is prerelease testing; bugs are a way of life. But you should check it out again when it's released as stable; the bugs are rapidly disappearing.

    With VBScript now on the way out and increasing hostility towards VBA making it more and more painful to use, it's a mistake to think that long term you won't need alternatives to VB6... and even if the compilers for other languages are more 'stable', you'd introduce far more bugs by human or AI error trying to port your project.

  6. #6
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    5,010

    Re: VB6 project not working "quite" right in tB (can't find the root cause)

    Quote Originally Posted by fafalone View Post
    *snip* you'd introduce far more bugs by human *snip* error trying to port your project.
    Which boils down to insufficient knowledge in both languages: Your "source"-language and your "target"-language.
    Last edited by Zvoni; Tomorrow at 31:69 PM.
    ----------------------------------------------------------------------------------------

    One System to rule them all, One Code to find them,
    One IDE to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    Code is like a joke: If you have to explain it, it's bad

  7. #7
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,839

    Re: VB6 project not working "quite" right in tB (can't find the root cause)

    Quote Originally Posted by jangle View Post
    Problems like these are the reason I decided not to switch to tB. A complex and rare bug in the compiler can ruin your entire project. The main thing in a compiler is reliability and predictability, for this reason I stayed with VB6.
    There are bugs in VB6 compiler we often hit. Just today had to deal with OOM during compilation of one of our projects. It's your codebase not big enough to feel the pain.

    Btw, there are bugs in gcc and clang too. Every sufficiently large codebase has to deal with bugs in their compilers and/or 3-rd party deps -- it's inevitable.

    cheers,
    </wqw>

  8. #8

    Thread Starter
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,332

    Talking Re: VB6 project not working "quite" right in tB (can't find the root cause)

    The latest tB version fixed the image orientation. Still no luck on the access violation when enumerating the metadata properties:

    Name:  MetaDataProperties_tB_2.jpg
Views: 155
Size:  53.7 KB

    The VB6 version enumerates exactly 120 properties every time.

  9. #9
    Hyperactive Member
    Join Date
    Dec 2020
    Posts
    273

    Re: VB6 project not working "quite" right in tB (can't find the root cause)

    Quote Originally Posted by VanGoghGaming View Post
    The latest tB version fixed the image orientation. Still no luck on the access violation when enumerating the metadata properties:

    Name:  MetaDataProperties_tB_2.jpg
Views: 155
Size:  53.7 KB

    The VB6 version enumerates exactly 120 properties every time.
    The problem is due to this code:

    Code:
    Private Function GetString(This As tHSTRING, sString As String) As HRESULT
        SysReAllocString VarPtr(sString), WindowsGetStringRawBuffer(This.hString, 0&)
    End Function
    `
    This procedure is being vtable swapped for the IStringLightWeight::GetString member, defined as:
    `HRESULT __stdcall GetString([out, retval] BSTR* out)`

    The problem here is, that VB ByRef semantics are not a perfect match for OUT-only parameters. For an OUT parameter, the caller can legitimately give you any invalid value on input and you're not meant to use or interpret it, only overwrite it with a result. But in your implementation, by passing sString to SysReAllocString, the input value IS being used, (and freed). The thing to remember is: ByRef is equivalent to [in, out] semantics, not [out].

    To correct the problem, do this:
    Code:
    Private Function GetString(This As tHSTRING, sString As String) As HRESULT
        PutMemPtr VarPtr(sString), vbNullPtr
        SysReAllocString VarPtr(sString), WindowsGetStringRawBuffer(This.hString, 0&)
    End Function
    `
    BTW, we dealt with this exact issue a lot in the tB ActiveX implementations, as many AX controls are C++ based and will (legitimately) pass junk/uninitialized values on input to OUT-only params.

  10. #10
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,839

    Re: VB6 project not working "quite" right in tB (can't find the root cause)

    Why not just sString = vbNullString instead of calling PutMemPtr? I mean it looks like PutMemPtr leaks a BSTR or is it on purpose?

    Edit: Oh, my bad. I thought it was a problem at callsite (not clearing out params before calling the method) but we are dealing with GetString *implementation* which does not clear its own [out] only parameter properly but insist on deallocating it as a BSTR whatever junk is passed in there.

    Yes, this is not something any VBx implementation/devs deal with often (or at all).

    cheers,
    </wqw>

  11. #11

    Thread Starter
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,332

    Thumbs up Re: VB6 project not working "quite" right in tB (can't find the root cause)

    Outstanding sleuthing, cheers Wayne, I wouldn't have figured this one out in a million years!

    I'm still not clear why this works in VB6 without the additional call to "PutMemPtr" though. I was under the impression that all function parameters are initialized with zero by default so there shouldn't have been any "junk" pointer in "sString" to begin with... Maybe this is a key difference between VB6 and tB where VB6 does sanitize function parameters before using them?

    In any case, I've rewritten the "GetString" function as follows in order to eliminate the overhead introduced by the additional call to "PutMemPtr":

    Code:
    Private Function GetString(This As tHSTRING, pStr As LongPtr) As HRESULT
        pStr = SysAllocString(WindowsGetStringRawBuffer(This.hString, 0&))
    End Function
    It seems to work great so I'm a happy camper once again!

  12. #12

    Thread Starter
    PowerPoster VanGoghGaming's Avatar
    Join Date
    Jan 2020
    Location
    Eve Online - Mining, Missions & Market Trading!
    Posts
    2,332

    Red face Re: VB6 project not working "quite" right in tB (can't find the root cause)

    I went ahead and tested this theory just for my own edification:

    Code:
    Private Function GetString(This As tHSTRING, sString As String) As HRESULT
        If StrPtr(sString) Then
            Debug.Print "Junk pointer!"
            PutMemPtr VarPtr(sString), vbNullPtr
        End If
        SysReAllocString(VarPtr(sString), WindowsGetStringRawBuffer(This.hString, 0&))
    End Function
    "Junk pointer!" is printed all the time in tB while VB6 never does it. So I guess this falls under the "99% compatibility" case between tB and VB6!

    I agree with wqweto though, this shouldn't be encountered very often in the wild. I suppose VB6 is going the extra mile with making sure the memory is zeroed out before use even though this "extra safety" comes at the cost of additional overhead.

    I'm happy with the solution so I've marked this thread as "Resolved" now.

  13. #13
    Hyperactive Member
    Join Date
    Dec 2020
    Posts
    273

    Re: [RESOLVED] VB6 project not working "quite" right in tB (can't find the root cause

    It will depend on how the compiler temp variables get issued/reused as to whether the value will contain junk or not. AFAIK VB6 does not guarantee that the out-only param is zeroed at the callsite - though it may happen to do so in this particular case. Ultimately the COM contract denotes that we don't need to zero it, and so it is better not to do so, which allows for better optimization opportunities (for when we turn on LLVM optimizations fully).

    FWIW, we could easily add an advanced compiler option to switch the behaviour ON/OFF for zeroing out params at the callsite.

  14. #14
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,839

    Re: [RESOLVED] VB6 project not working "quite" right in tB (can't find the root cause

    > AFAIK VB6 does not guarantee that the out-only param is zeroed at the callsite

    If it's a typelib declared interface with [out] only param AFAIR VB6 does make sure ref types (String, Variant, Object) are cleared at callsite as not to leak temp values lurking in there.

    The problem is when a manually converted typelib erroneously marks these params as [in, out] in which case VB6 does not clear anything and leaks might occur.

    cheers,
    </wqw>

  15. #15
    PowerPoster
    Join Date
    Jul 2010
    Location
    NYC
    Posts
    6,955

    Re: [RESOLVED] VB6 project not working "quite" right in tB (can't find the root cause

    VB6 does not allow use of Implements with any interface containing an out-only parameter. That's why it's frequently changed, not by mistake. Issues from this change are rare and can be worked around, so it's not worth making a separate version for everywhere it's used.
    Last edited by fafalone; Mar 23rd, 2025 at 01:55 PM.

  16. #16
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Location
    Sofia, Bulgaria
    Posts
    5,839

    Re: [RESOLVED] VB6 project not working "quite" right in tB (can't find the root cause

    I mean calling methods with [out] only params e.g. ID3D11Device::GetImmediateContext when it's declared to void GetImmediateContext([out] ID3D11DeviceContext **ppImmediateContext) in the typelib.

    This code

    Code:
        Dim pDevice As ID3D11Device
        Dim pContext As ID3D11DeviceContext
        
        pDevice.GetImmediateContext pContext
    . . . compiles to this

    Code:
    0040191E 8D 4D E4             lea         ecx,[pContext]  
    00401921 E8 1A F8 FF FF       call        ___vbaFreeObj (401140h)  
    00401926 8D 45 E4             lea         eax,[pContext]  
    00401929 50                   push        eax  
    0040192A 8B 45 E8             mov         eax,dword ptr [pDevice]  
    0040192D 8B 00                mov         eax,dword ptr [eax]  
    0040192F FF 75 E8             push        dword ptr [pDevice]  
    00401932 FF 90 A0 00 00 00    call        dword ptr [eax+0A0h]
    . . . i.e. pContext receiving variable is explicitly cleared at callsite for the [out] only parameter.

    cheers,
    </wqw>

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