🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Problem with string &out parameter on 2.1.0 WIP4.

Started by
2 comments, last by thebolt00 19 years, 3 months ago
Hi! I have an function which returns a std::string as an out parameter, and this is causing me some problems. I am using 2.1.0 WIP4. The string registration is taken more or less directly from the code provided with the library

std::string StringFactory (asUINT length, const char *s)
{
  return std::string (s);
}

void ConstructString (std::string *thisPointer)
{
  new(thisPointer) std::string ();
}

void DestructString (std::string *thisPointer)
{
  thisPointer->~string ();
}

std::string &AssignString (const std::string &other, std::string *thisPointer)
{
  new(thisPointer) std::string (other);
  return *thisPointer;
}

engine->RegisterObjectType ("String", sizeof (std::string), asOBJ_CLASS_CDA);
    engine->RegisterStringFactory ("String", asFUNCTION(StringFactory), asCALL_CDECL);
    engine->RegisterObjectBehaviour ("String", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST);
    engine->RegisterObjectBehaviour ("String", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST);
    engine->RegisterObjectBehaviour ("String", asBEHAVE_ASSIGNMENT, "String &f(String &in)", asFUNCTION(AssignString), asCALL_CDECL_OBJLAST);

In fact there are some more parts to it, but i don't think they should have any effect for this (if you suspect so, ask and i provide the rest of it). Then i have my method returning the reference

static bool Get (PropType &value, Property *property)
{
  value = "hi";
  return true;
}

engine->RegisterObjectType ("Property", sizeof (Property), asOBJ_CLASS);
engine->RegisterObjectMethod ("Property", "void Get (String &out)", asFUNCTION(GetString), asCALL_CDECL_OBJLAST);

The problem arises when i have code like this (in a method in my AS-script)

String outval;
property.Get (outval);

The method is called, and after that it tries to use my AssignString, but the first parameter is totaly bogus (0). Anyone else experiencing this? Is this a bug which can be confirmed, or am I doing something totaly wrong? Last piece of info, here is the output from the offending line (outputed by AS_DEBUG-compilation) (ok, in fact the line is a bit more complicated.. it gets the property handle on same line, but that does work so I did not include that above)

  178  37 *    SUSPEND
  182  37 *    PSF      4
  188  38 *    ALLOC    9, -23
  200  37 *    RDSF4    3
  206  38 *    RDSF4    4
  212  39 *    CALLSYS  -25           (String& ?(String&))
  220  37 *    SET4     0x4          (i:4, f:5.60519e-045)
  228  38 *    GETOBJREF 0
  234  38 *    STR      2         (l:5 s:"Title")
  240  40 *    CALLSYS  -22           (String ?(int, const uint8&))
  248  38 *    STOREOBJ 4
  254  38 *    SET4     0x4          (i:4, f:5.60519e-045)
  262  39 *    GETOBJ   0
  268  39 *    RDSF4    0
  274  40 *    CHKREF
  278  40 *    CALLSYS  -148           (Entity@ CastToEntity())
  286  39 *    STOREOBJ 5
  292  39 *    RDSF4    5
  298  40 *    CHKREF
  302  40 *    CALLSYS  -79           (Property@ GetProperty(String))
  310  38 *    STOREOBJ 6
  316  38 *    PSF      6
  322  39 *    PSF      5
  328  40 *    FREE     1
  336  39 *    RD4
  340  39 *    CHKREF
  344  39 *    CALLSYS  -44           (void Get(String&))
  352  37 *    SET4     0x4          (i:4, f:5.60519e-045)
  360  38 *    RDSF4    3
  366  39 *    GETOBJREF 1
  372  39 *    CALLSYS  -25           (String& ?(String&))
  380  37 *    PSF      4
  386  38 *    FREE     9
  394  37 *    PSF      6
  400  38 *    FREE     13

BR Mårten Svanfeldt
Advertisement
There is indeed a bug in the compiler. It seems the compiler is resuing a temporary variable before it is freed, making the string parameter you receive by reference invalid.

Could you please show the entire statement that you make on that line? And show me the signatures of all the functions you call? I need this so that I can recreate the problem and fix the bug.

It is the combination of the calls that make this bug show up. You ought to be able to work around it by breaking up the statement in several smaller pieces, storing the intermediate values in variables.

Regards,
Andreas


AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

I've fixed the bug. The fix involves quite a lot of changes though so, I won't post it here. If you need it I can send you the code for the new compiler.

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

Nah, I can wait for next WIP release, but thankyou anyhow.
(I prefer to be in sync with version officially avaiable:)

-Mårten

This topic is closed to new replies.

Advertisement