• Create Account

# '=' operator

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

4 replies to this topic

### #1abrken  Members   -  Reputation: 130

Like
0Likes
Like

Posted 30 September 2004 - 10:16 PM

Back on AngelScript integration after having other projects running ... Well, I've got a request or question : Working with 1.9.0 WIP 4 (I will as soon as I can integrate 1.9.2) I am having troubles with the '=' operator. I am using a class (that I have grabbed from the net) that is using CString. Peharps you don't know about CString, it's an MFC string class that is using reference counting for not duplicating strings but rather having reference counting. So, when you're making csTest1 = csTest2 , the CString = operator is called (MFC/SRC/STRCORE.CPP) :
const CString& CString::operator=(const CString& stringSrc)
{
if (m_pchData != stringSrc.m_pchData)
{
if ((GetData()->nRefs < 0 && GetData() != _afxDataNil) ||
stringSrc.GetData()->nRefs < 0)
{
// actual copy necessary since one of the strings is locked
AssignCopy(stringSrc.GetData()->nDataLength, stringSrc.m_pchData);
}
else
{
// can just copy references around
Release();
ASSERT(stringSrc.GetData() != _afxDataNil);
m_pchData = stringSrc.m_pchData;
InterlockedIncrement(&GetData()->nRefs);
}
}
return *this;
}

As you can see, the '=' operator use InterlockedIncrement to not duplicate the real string but rather increment a reference count. Ok, this is just to explain what's going on with AS. I have referenced a class like this :
	in_pAsEngine->RegisterObjectType("CSMTPAddress", sizeof(CSMTPAddress), asOBJ_CLASS);

As you can see, there is NO asBEHAVE_ASSIGNMENT behaviour declared. Now, this is the script :
CSMTPMessage msgSMTP;
CString csText;
CSMTPBodyPart csBodyPart;
CPJNSMTPConnection smtpCnx;

csText = "Bonjour d'AngelScript";
csBodyPart.SetTitle(csText);
csText = "BodyPart\nvia angelscript\net\nSMTP PJ Naughter";
csBodyPart.SetText(csText);

msgSMTP.m_Priority = 0;

if (smtpCnx.Connect("smtp.wanadoo.fr", 0, "", "", 25, "") == 1) {
smtpCnx.SendMessage(msgSMTP);
smtpCnx.Disconnect(1);
}

This code is well compiled and not so well executed. You can notice the :
msgSMTP.m_From = adrFrom;

The '=' operator is working but there is not '=' operator behaviour defined ! So, I don't know what AS is doing but the crash come at the msgSMTP destruction because, the CString that is member of CSMTPMessage is deleted and there is corrupted data in it. How did I solve the pb ? Simply in referencing the '=' operator behaviour that way :
CSMTPAddress *asCSMTPAddressCopy(const CSMTPAddress *src, CSMTPAddress *dst)
{
(*dst) = (*src);
return dst;
}

...


So, my request would be this one : Is this a normal feature of AS to have a default '=' operator ? If Yes, could it become No because it can cause some troubles on some referenced classes. If No, I think that you can fire a bug event ! Regards, AbrKen.

### #2Andreas Jonsson  Moderators   -  Reputation: 2966

Like
0Likes
Like

Posted 01 October 2004 - 03:25 AM

I know of the CString class, though I don't use it anymore. I've dropped MFC a long time ago.

The default assignment operator is a direct copy of data stored in the object. So it is only expected that you'd get problems if you don't register the assignment operator.

I noticed that you wrote a wrapper function for registering the assignment operator. That is actually not necessary, you could register the operator directly like this:

r = engine->RegisterObjectBehaviour("string", asBEHAVE_ASSIGNMENT, "string &f(const string &)", asMETHODPR(CString, operator =, (const CString&), CString&), asCALL_THISCALL); assert( r >= 0 );r = engine->RegisterObjectBehaviour("string", asBEHAVE_ADD_ASSIGN, "string &f(const string &)", asMETHODPR(CString, operator+=, (const CString&), CString&), asCALL_THISCALL); assert( r >= 0 );

What I can do to improve this behaviour is to have AngelScript verify that an object registered with the asOBJ_CLASS_ASSIGNMENT flag actually have the assignment behaviour registered as well. The same for asOBJ_CLASS_DESTRUCTOR and asOBJ_CLASS_CONSTRUCTOR.
AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

### #3abrken  Members   -  Reputation: 130

Like
0Likes
Like

Posted 01 October 2004 - 04:04 AM

Quote:
 Original post by WitchLordThe default assignment operator is a direct copy of data stored in the object. So it is only expected that you'd get problems if you don't register the assignment operator.

Ok, this is from where comes the problem !

This mean that if I have :
class A { B *ptrMember; int intMember;public: A() {ptrMember = NULL;}; virtual ~A() {if (ptrMember) delete ptrMember;}; void DoSomeStuffThatFillptrMember() {ptrMember = new B();};}

and a script where class A have been referenced :
void scriptFunc(){  A var1, var2;  var1.DoSomeStuffThatFillptrMember();  var2 = var1;}

At the end of the script function when var2 and var1 call their destructors, there should be a crash.

Is that correct ?

### #4Andreas Jonsson  Moderators   -  Reputation: 2966

Like
0Likes
Like

Posted 01 October 2004 - 05:41 AM

Yes, that is correct.

Do you think I need to add some way of disabling the assignment operator? Isn't it better to define an assignment operator that does a correct assignment or sets a script exception?

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

### #5abrken  Members   -  Reputation: 130

Like
0Likes
Like

Posted 01 October 2004 - 08:03 PM

To be honest, I don't know what is the good answer to this.
Having the '=' operator always defined is a good feature but, that should be avoided when the object stores some values in it's data members that needs to be 'copied' and not 'memcpy'.
So how AS can handle this ?
I see 2 solutions :
- Avoid the '=' operator when not defined as a behaviour.
- Don't change anything.

The first solution lead AS user to change their AS integration when they were using '=' and not defining the behaviour. This lead to insatisfaction of some AS users and this is bad.
So you can add to AS the Not behaviour that let user define that an object do not implement the '=' behaviour. This is much better since this is an add on and not a change to the library.

You can also don't change anything and put a big warning to AS users, because even with some class where there is no pointer, this kind of crash can happen.

Really, do what you feel.

Now that I have understood the '=' feature, I can understand some crash that were happening and that I was solving in using pointers rather than script constructed objects !

AbrKen.

Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

PARTNERS