Registering a custom string class

Started by
1 comment, last by maedhros777 13 years, 11 months ago
Hi, I've been trying to register my own string class named asString in AngelScript, and I have a couple questions. I originally encountered a segmentation fault when using operator+ with my class (by the way, I run Linux), but when I changed the operator+ function to have a return type of asString * instead of asString it worked. Why is that? Shouldn't that cause memory leaks? For example: string a = "Hello "; string b = "world!"; string c = a + b; Wouldn't that leak memory, and not work as well, since c is of type string and "a + b" is of type string *? Unless there's something I'm not understanding about AS and/or pointers. But it compiles and runs fine. By the way, it's the same situation with asIScriptEngine->RegisterStringFactory. Thank you for your help. EDIT: Oh yeah, I forgot to say that asString is a reference type.
Advertisement
All string are internally asString*, and asString are reference counted (did you do that? ... because it was supposed you to do it).

http://en.wikipedia.org/wiki/Reference_counting
Oh, okay, I get it now. So then all reference types are dynamically allocated, and are deleted when they have no more references? I did make asADDREF and asRELEASE behaviours, by the way. I was just getting confused because I was seeing new without a nearby delete and the tutorial I was reading didn't explain it well.

This topic is closed to new replies.

Advertisement