# assign Operator Issues

This topic is 1317 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi,

Working to implement AngelScript into my game project, but I'm having issues with the assign operator - using it results in a debug assertion error with "Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)

I'm trying to use GLM with my scripts, so I've made a VEC2_REF struct to interface between GLM and AngelScript, that holds the required addref and release functions. This is also where I have the operator = overload - basicly it just calls GLM's own operator overload.

struct VEC2_REF : public REF_COUNTER // REF-COUNTER contains addref and release functions
{
VEC2_REF( vec2 v )
:	v( v )
{}

vec2 v;

VEC2_REF &operator=( VEC2_REF &r)
{
if( this != &r )
v = r.v; // Call GLM's operator overload

return *this;
}
};


This is how I'm registering the object to angelscript

engine->RegisterObjectType( "vec2", sizeof(VEC2_REF), asOBJ_REF );

...

/* Various behavior functions - not important for this */

/* Various operator overloads being registered... */

engine->RegisterObjectMethod( "vec2", "vec2 @opAssign(vec2&)", asMETHODPR( VEC2_REF, operator=, (VEC2_REF&), VEC2_REF& ), asCALL_THISCALL );

/* Various extra functions... */

...

// Vec2 properties NB: offset with sizeof(int) to skip over the nRefs variable in REF_COUNTER class
engine->RegisterObjectProperty( "vec2", "float x", sizeof(int) + sizeof(float) * 0 );
engine->RegisterObjectProperty( "vec2", "float y", sizeof(int) + sizeof(float) * 1 );


And this is the script itself

void main()
{
vec2 a;
a.x = 2.0f;
a.y = 5.0f;
vec2 b( 7, 10 );

log( a );
log( b );

a = b;

log( a );
}


The weird is that it doesn't actually crash on a = b; However it only the error only occurs IF the assign is there.

Also the last log call will output something weird if the assign operator have been used.

v( 2 5 )
v( 7 10 )
v( -1.59e+038 -1.59e+038 )


I really can't figure out what I'm doing wrong, or why this is happening; all other operators are fine. Any help would be highly appreciated.

mSkull

Edited by mSkull

##### Share on other sites

Your VEC2_REF operator= is implemented to return the type by reference without incrementing the refcount, but you've registered this method as returning the type as a handle. Because of this AngelScript will release the returned reference when it is done with it, and this cause the VEC2_REF instance to be destroyed while you still have other pointers referring to it elsewhere. This is why you're getting the 'Expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse)' error.

Do you have a specific reason for implementing the vec2 type as a reference type? Unless you have a specific reason to have these instances allocated on the heap with reference counting, it is probably better to register the vec2 class directly as a value type (similar to the complex type in the scriptmath add-on).

Regards,

Andreas

##### Share on other sites

Thank you so much for your help - highly appreciated :D

The reason I'm using reference type is because I misunderstood the manual, and thought I was suppose to be doing that. I'm already working on swapping that around, so thanks for the heads up.