Announcement

Collapse
No announcement yet.

Crash on string assignment!

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Crash on string assignment!

    Hi,

    I have a simple dll with one exported function which calls a callback function in the client application passing a pointer to a UDT having two fields.

    All works well. Tested with clients written in both Purebasic (the language in which the dll is created in) and Powerbasic.

    However, in Powerbasic I am getting a crash on a very strange line and I cannot figure out quite why?

    The UDT in question has a field which points to a character array (of unknown length). When I access this string via Peek$() then no problem and no crashes. However, I am trying to access the string through a pointer because of the unknown length etc. I am aware of the potential pitfalls of doing this, but I thought that I had taken care to avoid those.

    Code:
    #Compile Exe
    #Dim All
    
    Type test
      a As Long
      b As Long     'Points to a null-terminated character array of arbitrary length.
    End Type
    
    
    Declare Sub InvokeCallback Lib "dll5.DLL" Alias "InvokeCallback" (ByVal address As Long)
    
    
    'The following is called by the dll.
    Sub myCallback(structure As test)
        Local str1 As String, ptrString As Long Ptr
        Local x As String
    
        'Place the address of the character array into the str1 string.
            ptrString = VarPtr(str1)
            @ptrString = structure.b
        'Let us take a look.
            ? str1          'Fine.
            Let x = str1    'CRASH!
    
        @ptrString = 0  'Avoid Powerbasic attempting to free the dll string.
    End Sub
    
    
    Function PBMain () As Long
        InvokeCallback(CodePtr(myCallback))
    End Function
    The code compiles ok and the string passed from the dll is retrieved fine. However, the crash occurs on the simple assignment Let x = str1 which is puzzling!

    If I use a proper bona-fide string pointer with two levels of indirection then all works fine. But I do not understand why the above code should crash on the simple string assignment? Is Powerbasic attempting to release the memory in str1 via SysFreeString()? This would certainly explain the crash, but it wouldn't make sense for PB to do this!

    Much appreciated if someone can shed some light on this? I can make the dll available if it is required.

    Thanks.

    Stephen.
    Last edited by Stephen Rodriguez; 5 Sep 2009, 08:03 AM.
    http://www.arctic-reports.com/
    http://www.nxsoftware.com

  • #2
    Okay,

    the best way of accessing the string seems to be through an Asciiz pointer which is fine - nice solution.

    Still, wouldn't mind knowing why the code above crashes?
    http://www.arctic-reports.com/
    http://www.nxsoftware.com

    Comment


    • #3
      When you corrupt memory, the crash can happen at any place.

      You must use ASCIIZ, because PureBasic doesn't use OLE strings.
      Forum: http://www.jose.it-berater.org/smfforum/index.php

      Comment


      • #4
        >Still, wouldn't mind knowing why the code above crashes

        That's simple. You made an error

        Code:
        'Place the address of the character array into the str1 string.
                ptrString = VarPtr(str1)
                @ptrString = structure.b
        This code does not put the structure data into the string data. ptrString is not the address of the data, it's the address of the handle. You need to
        A) make ptrStr = STRPTR (str1)
        B) ensure LEN(str1) >= SIZEOF(structure.b) (or else structure.b don't fit). You need to initialize that string; as written it has a length of zero.

        I can't tell what you are trying to return to PBMAIN(), but I will bet there is a better way to do it, and I think the "ASCIIZ PTR" datatype will be part of that better way.

        Show code for the "InvokeCallback" procedure and someone will figure it out.

        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Thanks for the replies.

          Michael, I don't think that is it because a BSTR is simply a pointer to a charcter buffer prepended with 4-bytes... ah bugger.... that's it!

          You must use ASCIIZ, because PureBasic doesn't use OLE strings.
          That is it! Purebasic strings are not prepended with the aforementioned 4-bytes!

          Tested and yep it works. Just made sure there were the 4-bytes containing the length of the string prepended to the character array and it works fine!

          But the message is simple, I shall use an asciiz pointer for this!

          Always good to know why things do not work sometimes as much as it is to know why they do work!

          Got to say that I love Powerbasic's use of pointers - brilliant.

          Thanks again.
          Last edited by Stephen Rodriguez; 5 Sep 2009, 09:17 AM.
          http://www.arctic-reports.com/
          http://www.nxsoftware.com

          Comment


          • #6
            >Got to say that I love Powerbasic's use of pointers - brilliant.

            The PowerBASIC compiler does not use pointers... it supports pointer variables. Yes, a Good Thing. But it is not the compiler which makes applications programs work.. or not.

            It has been said "'Tis a poor workman indeed who blames his tools." I think the converse true as well.

            MCM
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Originally posted by Michael Mattias View Post
              >Got to say that I love Powerbasic's use of pointers - brilliant.

              The PowerBASIC compiler does not use pointers... it supports pointer variables. Yes, a Good Thing. But it is not the compiler which makes applications programs work.. or not.

              It has been said "'Tis a poor workman indeed who blames his tools." I think the converse true as well.

              MCM
              Yes, but I think you know what I meant to say.

              Not sure I was blaming the tools anywhere (though I suspect that is not what you are saying!) - all I am doing is learning this language and liking what I find. I do not expect to become as accomplished with Powerbasic overnight as I would argue that I might be with some other languages, but I'm getting there! Best way for me to learn is to experiment and poke away to see just how far I can push before I fall over the edge!

              The languages I use thus far (mostly) do not offer pointers with the flexibility and levels of indirection that PowerB does, and that opens up new ways of doing things for me. Hence this posting which represents me just experimenting, whilst also testing out features which I am in need of.
              http://www.arctic-reports.com/
              http://www.nxsoftware.com

              Comment


              • #8
                I get what Stephen is saying, and much in the same boat myself. (A current project works fine with all PB vs PB-dll, but DANGED if I know why the same DLL in a VB environment crashes????)

                Often it is hard to track down a "Bug" of sorts when mixing how one compiler handles things and how another does and finding the "Rosetta-Stone" of what is the PROPER way to handle things, because they most likely do not show their ugly head until welllllllll AFTER where things got corrupted.
                Engineer's Motto: If it aint broke take it apart and fix it

                "If at 1st you don't succeed... call it version 1.0"

                "Half of Programming is coding"....."The other 90% is DEBUGGING"

                "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                Comment


                • #9
                  Cliff, does VB not require strings to be passed in Unicode format?

                  That is if your dll is passing a string (via a buffer or some such) to the host VB application, will VB not require that string in Unicode format as opposed to Powerbasic's Ascii encoded strings?
                  http://www.arctic-reports.com/
                  http://www.nxsoftware.com

                  Comment

                  Working...
                  X