🎉 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!

assign Operator Issues

Started by
1 comment, last by mSkull 9 years, 11 months ago

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

Advertisement

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

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

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.

This topic is closed to new replies.

Advertisement