The problem lies with how you registered the CString type. I'm betting that your CString C++ class has a constructor, a destructor, and maybe even an overloaded assignment operator. If that is so you need to register the type with the asOBJ_CLASS_CDA flag. This flag is actually a shortcut for asOBJ_CLASS | asOBJ_CLASS_CONSTRUCTOR | asOBJ_CLASS_DESTRUCTOR | asOBJ_CLASS_ASSIGNMENT.
What is happening is that your StringFactory function is expecting a hidden pointer to be passed to it before the length parameter. This pointer holds the location where the returned CString should be initialized. But since you didn't register the CString type with asOBJ_CLASS_CDA AngelScript has no way of knowing that your C++ function wants that hidden pointer. You can verify this situation by converting the length parameter to char *, you'll see that it is actually the pointer to the string buffer.
I'll make a note in the manual about this very common mistake.
If only there where some way of autodetecting the correct flag to use when registering a type.
Your ConstructString() function is wrong. It should be:
#include <new.h> // Include placement newvoid ConstructString(CString &self){ new(&self) CString;}
Your AssignString() function is also wrong. It should be:
CString &AssignString(CString &other, CString &self){ return self = other;}
It would also be possible to register the overloaded assignment operator directly using asCALL_THISCALL.
The following code shows how to register std::string for use with AngelScript. I'm sure you can adapt this for your CString class.
[AngelScript 1.10.0 WIP 4]
#include "angelscript.h"#include <assert.h>#include <string>using namespace std;static string StringFactory(asUINT length, const char *s){ return string(s);}static void ConstructString(string *thisPointer){ new(thisPointer) string();}static void DestructString(string *thisPointer){ thisPointer->~string();}void RegisterStdString(asIScriptEngine *engine){ int r; // Register the bstr type r = engine->RegisterObjectType("string", sizeof(string), asOBJ_CLASS_CDA); assert( r >= 0 ); // Register the bstr factory r = engine->RegisterStringFactory("string", asFUNCTION(StringFactory), asCALL_CDECL); assert( r >= 0 ); // Register the object operator overloads r = engine->RegisterObjectBehaviour("string", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ConstructString), asCALL_CDECL_OBJLAST); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("string", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(DestructString), asCALL_CDECL_OBJLAST); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("string", asBEHAVE_ASSIGNMENT, "string &f(const string &)", asMETHODPR(string, operator =, (const string&), string&), asCALL_THISCALL); assert( r >= 0 ); r = engine->RegisterObjectBehaviour("string", asBEHAVE_ADD_ASSIGN, "string &f(const string &)", asMETHODPR(string, operator+=, (const string&), string&), asCALL_THISCALL); assert( r >= 0 ); // Register the global operator overloads r = engine->RegisterGlobalBehaviour(asBEHAVE_EQUAL, "bool f(const string &, const string &)", asFUNCTIONPR(operator==, (const string &, const string &), bool), asCALL_CDECL); assert( r >= 0 ); r = engine->RegisterGlobalBehaviour(asBEHAVE_NOTEQUAL, "bool f(const string &, const string &)", asFUNCTIONPR(operator!=, (const string &, const string &), bool), asCALL_CDECL); assert( r >= 0 ); r = engine->RegisterGlobalBehaviour(asBEHAVE_LEQUAL, "bool f(const string &, const string &)", asFUNCTIONPR(operator<=, (const string &, const string &), bool), asCALL_CDECL); assert( r >= 0 ); r = engine->RegisterGlobalBehaviour(asBEHAVE_GEQUAL, "bool f(const string &, const string &)", asFUNCTIONPR(operator>=, (const string &, const string &), bool), asCALL_CDECL); assert( r >= 0 ); r = engine->RegisterGlobalBehaviour(asBEHAVE_LESSTHAN, "bool f(const string &, const string &)", asFUNCTIONPR(operator <, (const string &, const string &), bool), asCALL_CDECL); assert( r >= 0 ); r = engine->RegisterGlobalBehaviour(asBEHAVE_GREATERTHAN, "bool f(const string &, const string &)", asFUNCTIONPR(operator >, (const string &, const string &), bool), asCALL_CDECL); assert( r >= 0 ); r = engine->RegisterGlobalBehaviour(asBEHAVE_ADD, "string f(const string &, const string &)", asFUNCTIONPR(operator +, (const string &, const string &), string), asCALL_CDECL); assert( r >= 0 ); // Register the object methods r = engine->RegisterObjectMethod("string", "uint length()", asMETHOD(string,size), asCALL_THISCALL); assert( r >= 0 );}