Sign in to follow this  
xivVerge

Convert array to string

Recommended Posts

xivVerge    122
Hi all, in one of my scripts I have a class called A that contains some members. When creating an array of A and converting this array to a string I get a access violation in asCScriptEngine::CallObjectMethod Here is a small example. The string is the stl string registered without reference counting:
class A {
    int x;
    int y;
}

string toString(const A& a) {
    return a.x + ":" + a.y;
}

string toString(const A[]& a) {
    string s;
    for (uint i=0; i<a.length(); i++)
        s += "element " + i + ":" + toString(a[i]);
    return s;
}

A[] As;

As.resize(2);
As[0].x = 1;
As[0].y = 2;
As[1].x = 1;
As[1].y = 2;

print(toString(As));


Any ideas whats going wrong. Regards, Tim

Share this post


Link to post
Share on other sites
WitchLord    4678
I'll have to investigate this. It could be a problem in AngelScript itself. However, as I'm on vacation (just doing a quick stop by on the forum) don't expect any resolution until I get home in January.

Are you using CScriptString as the string implementation?

Share this post


Link to post
Share on other sites
xivVerge    122
Hi,

no I've registered the std::string directly with:

RegisterObjectType("string", sizeof(string), asOBJ_VALUE | asOBJ_APP_CLASS_CDA);
RegisterStringFactory("string", asFUNCTION(StringFactory), asCALL_CDECL);

Everything else connected to string is working fine (string return value, pass string by value and ref...)

Have a nice vacation,

Tim

[Edited by - xivVerge on December 22, 2008 1:21:11 AM]

Share this post


Link to post
Share on other sites
xivVerge    122
I did another investiagtion.
It has nothing to do with string.
The following example gives the same error.


class A
{
int x;
}

int sum(const A[]& a)
{
int s = 0;
for (uint i=0; i<a.length(); i++)
s+=a[i].x;
return s;
}

A[] As;

As.resize(2);
As[0].x = 1;
As[1].x = 2;

sum(As);




Merry Christmas,
Tim

Share this post


Link to post
Share on other sites
WitchLord    4678
Just got a chance to look into this, and unfortunately both of the examples that you're presenting are working without any error, so I have no idea what's going on. This is on the latest version from the SVN (2.15.1 WIP).

What version of AngelScript are you using?

Share this post


Link to post
Share on other sites
WitchLord    4678
I wrote the following test case, and tried it with AS_NO_USER_ALLOC, but it still doesn't show any problem. The fact that you use Posix threads shouldn't make a difference either. But I'll give the test a try on my Mac as well when I get the time, just to make sure.


bool Test2()
{
bool fail = false;

const char *script =
"class A \n"
"{ \n"
" int x; \n"
"} \n"
"int sum(const A[]& a) \n"
"{ \n"
" int s = 0; \n"
" for (uint i=0; i<a.length(); i++) \n"
" s+=a[i].x; \n"
" return s; \n"
"} \n";

const char *exec =
"A[] As; \n"
"As.resize(2); \n"
"As[0].x = 1; \n"
"As[1].x = 2; \n"
"sum(As); \n";

asIScriptEngine *engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
asIScriptModule *module = engine->GetModule("module", asGM_ALWAYS_CREATE);

module->AddScriptSection("script", script);
int r = module->Build();
if( r < 0 )
{
fail = true;
}

r = engine->ExecuteString("module", exec);
if( r != asEXECUTION_FINISHED )
{
fail = true;
}

engine->Release();

return fail;
}



Could you verify if the above test reproduces the problem on your setup?

Regards,
Andreas

Share this post


Link to post
Share on other sites
xivVerge    122
I tried your example in a little console program and it worked.
But when I run the script in my other project, where many different classes are registered it always crashes.
I will look into it and maybe find some hints to the bug.

Thanks for your help,
Tim

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this