• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0

Memory leak when doing an assign operation with a handle

7 posts in this topic

Quickly summarized, the problem is that when I assign a value from a c++ created object (handle) to a script created one, memory is not freed properly.

The code looks like this:
[code]cBoundingVolume@ pCharBV = GetBoundingVolume();
cBoundingVolume bvCopy = pCharBV;[/code]

This does not release the memory of cBoundingVolume (I am not sure of the exact script place the memory allocation takes place but can try and find out if needed) and I get a memory leak.

However,this code works just fine:

[code]cBoundingVolume@ pCharBV = GetBoundingVolume();
cBoundingVolume bvCopy;[/code]

GetBoundingVolume is a c++ function and has this declartion:
[code]cBoundingVolume@+ GetBoundingVolume()[/code]

I am guessing that the problem might be that I am not declaring the copy operator correctly?
Currently it looks like this:
[code]RegisterOperator("cBoundingVolume &opAssign(const cBoundingVolume &in)", asMETHODPR(cBoundingVolume, operator =, (const cBoundingVolume &), cBoundingVolume &) );[/code]
Do I need to do something different for it to work? (as in increasing a user count in cBoundingVolume?)

I was not sure exactly what code is needed, so just say if you need to see more in order to locate the problem!

Share this post

Link to post
Share on other sites
I'll look into this. I made some changes in version 2.22.1 to avoid making unnecessary copies with value assignments. It is quite possible that this has broken something.

The opAssign method shouldn't increase the refCount as it only works directly with references and not handles.

The cBoundingVolume is obviously a asOBJ_REF type. Does it have any other flags? Have you registered a copy constructor for it? Does it have a default constructor?

Share this post

Link to post
Share on other sites
I wasn't able to reproduce this problem either. It is possible the problem has been fixed in the latest SVN already.

This is the test case I wrote:


engine = asCreateScriptEngine(ANGELSCRIPT_VERSION);
bout.buffer = "";
engine->SetMessageCallback(asMETHOD(CBufferedOutStream, Callback), &bout, asCALL_THISCALL);

asIScriptModule *mod = engine->GetModule("", asGM_ALWAYS_CREATE);
"class T { \n"
" T() {} \n"
// " T(int v) {} \n"
" T &opAssign(const T&in o) {return this;} \n"
"} \n"
"T @Get() { return T(); } \n"
"void main() { \n"
" T @t1 = Get(); \n"
" T t2 = t1; \n"
"} \n");

r = mod->Build();
if( r < 0 )

if( bout.buffer != "" )
printf("%s", bout.buffer.c_str());

r = ExecuteString(engine, "main()", mod);


The main() function in the script produces the following bytecode:


void main()

Temps: 2

001: T@ t1
003: T t2
002: T {noname}

0 0 * PUSH 3
- 7,3 -
2 3 * VarDecl 0
2 3 * CALL 34 (T@ Get())
4 3 * STOREOBJ v2
5 3 * PshVPtr v2
6 4 * PSF v1
7 5 * REFCPY 0xc9cde8
9 4 * FREE v2, 13225448
11 4 * POP 1
- 8,3 -
12 3 * SUSPEND
13 3 * VarDecl 1
13 3 * ChkNullV v1
14 3 * VAR v1
15 4 * CALL 32 (T@ T())
17 4 * STOREOBJ v3
18 4 * PshVPtr v3
19 5 * GETOBJREF 1
20 5 * CALLINTF 36 (T& T::opAssign(const T&in))
- 9,2 -
22 3 * SUSPEND
23 3 * FREE v3, 13225448
25 3 * FREE v1, 13225448
27 3 * 0:
27 3 * RET 0

Can you show me the bytecode that is produced by your script? It should be similar to the above. If you use the debug version of the scripting library it will write a file with the bytecode for each compiled function in the directory AS_DEBUG.

Share this post

Link to post
Share on other sites
I only use [color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]asOBJ_REF as flag.[/left][/size][/font][/color]

[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left]Here are the factories:[/left][/size][/font][/color]
[color=#282828][font=helvetica, arial, verdana, tahoma, sans-serif][size=3][left][code][/left][/size][/font][/color]RegisterFactory("", Factory_cBoundingVolume_Default);
RegisterFactory("const cVector3f &in avMin, const cVector3f &in avMax", Factory_cBoundingVolume_MinMax);
RegisterFactory("const cVector3f &in avSize", Factory_cBoundingVolume_Size);[/code]

Here is the script:
[code]void main()
cBoundingVolume @pBV = GetBV();
cBoundingVolume copyBV = pBV;


Here is the bytecode output:

[code]void main()

Temps: 2, 10

001: cBoundingVolume@ pBV
003: cBoundingVolume copyBV
002: cBoundingVolume {noname}
010: tString {noname}

0 0 * PUSH 10
- 3,2 -
1 10 * SUSPEND
2 10 * VarDecl 0
2 10 * CALLSYS 4125 (cBoundingVolume@ GetBV())
4 10 * STOREOBJ v2
5 10 * PshVPtr v2
6 11 * PSF v1
7 12 * REFCPY 0x3dc6598
9 11 * FREE v2, 64775576
11 11 * POP 1
- 4,2 -
12 10 * SUSPEND
13 10 * VarDecl 1
13 10 * ChkNullV v1
14 10 * VAR v1
15 11 * CALLSYS 218 (cBoundingVolume@ _beh_2_())
17 11 * STOREOBJ v3
18 11 * PshVPtr v3
19 12 * GETOBJREF 1
20 12 * CALLSYS 227 (cBoundingVolume& cBoundingVolume::opAssign(const cBoundingVolume&in))
- 6,2 -
22 10 * SUSPEND
23 10 * STR 0 (l:6 s:"Done!
24 12 * PSF v10
25 13 * CALLSYS 351 (tString _string_factory_(const int, const uint8&))
27 10 * ObjInfo v10, 1
27 10 * PSF v10
28 11 * CALLSYS 4123 (void Print(tString&in))
30 10 * PSF v10
31 11 * CALLSYS 343 (void tString::_beh_1_())
- 7,2 -
33 10 * ObjInfo v10, 0
33 10 * SUSPEND
34 10 * FREE v3, 64775576
36 10 * FREE v1, 64775576
38 10 * 0:
38 10 * RET 0[/code]

Share this post

Link to post
Share on other sites
The bytecode produced looks ok. It is freeing the objects properly.

I think the problem might be in your implementation of the opAssign method.

Are you by any chance copying the ref counter too? That mustn't be done as it would screw up the memory management.

Share this post

Link to post
Share on other sites
Late reply

[quote name='Andreas Jonsson' timestamp='1326489145' post='4902453']
Are you by any chance copying the ref counter too? That mustn't be done as it would screw up the memory management.

Ah yes! That was the problem! Thanks!

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0