Results 1 to 6 of 6

Thread: VB / C++ COM BSTR question

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2002
    Posts
    2

    VB / C++ COM BSTR question

    Not sure if this is more a VB or C++ question, posted on both.

    I have a C++ dll with a function that is used
    in VB code.

    The C++ function returns a BSTR to the VB calling functions. This function is called ALOT by various VB function.

    The problem:

    After much processing (1 hour+), memory is consumed like crazy, 200M and up. We (the team that must debug this) think the BSTR's aren't being free'd in VB. We know this should be done automagically. I will test this theory today.

    If that is the case how can we free the BSTR memory in the VB code. I only know of:
    bstrVar = Nothing
    or
    bstrVar = ""

    Could I create a method in the C++ class that returns the memory with SysFreeString? May get a pointer from the VB code as a func parameter?

    any ideas are welcome.

  2. #2
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    to free string mem in VB, just assign it a zero-length value

    VB Code:
    1. Dim myStr As String
    2.  
    3. myStr = "hello"
    4.  
    5. 'free it
    6. myStr = ""


    i think in c++, you do

    PHP Code:
    char *mystring = new char[<maxlength>];

    //free if
    free (mystring
    i haven't use BSTR, so I wouldn't know..sorry
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

  3. #3
    Zaei
    Guest
    Take the return string as a parameter to the DLL function. That way, it is up to VB to create and delete the string.

    Z.

  4. #4
    Monday Morning Lunatic parksie's Avatar
    Join Date
    Mar 2000
    Location
    Mashin' on the motorway
    Posts
    8,169
    Code:
    char *mystring = new char[<maxlength>];
    
    //free if
    free (mystring)
    Ick ick ick
    Code:
    char *mystring = new char[<maxlength>];
    
    //free if
    delete[] mystring;
    Don't use free and new on the same pointer.
    I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
    -- Linus Torvalds

  5. #5
    Fanatic Member THEROB's Avatar
    Join Date
    Oct 2000
    Location
    I'm cold and there are wolves after me
    Posts
    575
    Try setting the object to nothing

    So:

    set obj = createobject("the.c++object")

    ret = obj.thefunctioncall

    set obj = nothing

    this should free the c++ object.


    Bye the way, the BSTR is only 65537 (In borland anyway) characters long so if you need any more you need to write this to a file.
    My secretary hopes that I will pay her, her landlord hopes that she will produce some rent, the Electricity Board hopes that he will settle their bill, and so on. I find it a wonderfully optimistic way of life. [Dirk Gently]

  6. #6
    Fanatic Member nabeels786's Avatar
    Join Date
    Jul 2001
    Location
    New York
    Posts
    919
    Originally posted by parksie
    Code:
    char *mystring = new char[<maxlength>];
    
    //free if
    free (mystring)
    Ick ick ick
    Code:
    char *mystring = new char[<maxlength>];
    
    //free if
    delete[] mystring;
    Don't use free and new on the same pointer.
    ah okay...maybe thats why...

    i learned something today
    Visit www.fragblast.com
    Gaming, forums, and a online RPG/Battle system




    (__Flagg) DOT NET? is this a Hindi Dating service?

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