Jump to content

  • Log In with Google      Sign In   
  • Create Account


- - - - -

bstr vs. int8*


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
2 replies to this topic

#1 leblebi   Members   -  Reputation: 124

Like
Likes
Like

Posted 01 April 2004 - 08:41 AM

Consider the following script:
t_img *tga;
bstr origname = "reflect.tga";
tga = img_new_tga ( bstr_to_int8(origname) );
and the functions:
t_img* img_new_tga (char *fname)
{
//blah blah

}
char* bstr_to_int8 ( asBSTR& str )
{
return (char*)str;
}
This works fine. But it doesnt work if I do this:
tga = img_new_tga ( "reflect.tga" );
or this
tga = img_new_tga ( bstr_to_int8("reflect.tga") );
Is this the expected behaviour? and if it is, is there a way to eliminate the need for the "bstr_to_int8" function. Typecasting in script doesnt work, it says that "it cant implicitly typecast from bstr to int8*". Thanks for any help.

Sponsor:

#2 Andreas Jonsson   Moderators   -  Reputation: 3309

Like
Likes
Like

Posted 01 April 2004 - 09:08 AM

Try registering


t_img* img_new_tga (char *fname)
{
//blah blah
}


like this:


engine->RegisterGlobalFunction("t_img *img_new_tga(bstr)", asFUNCTION(img_new_tga), asCALL_CDECL);


This should let you write your script like this:


t_img *tga;
tga = img_new_tga("reflect.tga");


BSTR is typedefed as (unsigned char *) so that it can be sent to functions taking a (char *) directly.

---

As to your other question. The behaviour is expected, since your bstr_to_int8() function takes a non const reference. Simply declare the parameter as const and you should be able to call it with a constant directly.

---

Currently the bstr needs to be handled with care. This is because AngelScript has to manage the memory allocated for the strings. Thus there are a few rules that you ought to follow or you''ll get memory leaks:

- When receiving an asBSTR by value, you must free the memory before returning from the function. Call asBStrFree()
- When returning an new asBSTR to the script engine it must be allocated with asBStrAlloc()
- When receiving an asBSTR & or an asBSTR * you don''t have to free the memory since the script engine is still holding a reference to it
- If you receive an asBSTR *, and need to change the string it points to call asBStrAlloc() for the new string, and asBStrFree() on the old string.

In a future version I will break out the implementation of asBSTR from the script engine so that each application can decide how they are handled. Of course I will make the functions needed to make the asBSTR work as normal available to the public for free.


__________________________________________________________
www.AngelCode.com - game development and more...
AngelScript - free scripting library

#3 leblebi   Members   -  Reputation: 124

Like
Likes
Like

Posted 01 April 2004 - 09:31 AM

Ahhh, ok. This,

AS_API asBSTR AS_CALL asBStrAlloc(asUINT length)
{
unsigned char *str = new unsigned char[length+4+1];
*((asUINT*)str) = length; // Length of string

str[length+4] = 0; // Null terminated


return str + 4;
}

explains it. Thanks a lot.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS