Sorry, come back with this one :)
I've a class pointer allocated in c++ code. I want to allow scripts to get this pointer to handle it, but don't allow script to create instance of that class.
So in c part, I've something like :
class MyClass
{
public:
MyClass() : m_refCount( 1 ) {}
int m_refCount;
... AddRef() & Release()
}
MyClass* GetMyClassPtr()
{
if( g_pMyClass )
g_pMyClass->AddRef();
return g_pMyClass;
}
...
// somewhere in the asEngine registering part
pEngine->RegisterObjectType( "MyClass", 0, asOBJ_CLASS ); // no C D or A since I don't want script to create it
pEngine->RegisterObjectBehaviour( "MyClass", asBEHAVE_ADDREF, "void f()", asMETHODPR(Myclass, AddRef, (), void), asCALL_THISCALL );
pEngine->RegisterObjectBehaviour( "MyClass", asBEHAVE_RELEASE, "void f()", asMETHODPR(MyClass, Release, (), void), asCALL_THISCALL );
pEngine->RegisterGlobalFunction( "MyClass@ GetMyClassPtr()", asFUNCTION(GetMyClassPtr), asCALL_CDECL );
Then in the script :
void main()
{
MyClass@ pMyClass = GetMyClassPtr();
}
Then, after running the script, I run in crash... (seems to be a bad pointer access).
After some tries, I found that after running the script, the refCount of g_pMyClass is 0, so I guess it has been deleted (by the Release() method), but why ? I stepped into GetMyClassPtr() and after calling AddRef(), the refCount is 2. I also tried to add assignment operator, but same result. I could make it reference instead of object handle, but i would be able to assign a variable later than in its declaration like :
MyClass& pMyClass;
...
pMyClass = GetMyClassPtr();
which is what I want (and what's worked well with pointers :)).
Any Idea ?
Lbas