| 1 |
<xml> |
| 2 |
<head> |
| 3 |
<title>Warnings and Gotchas of using ClanLib</title> |
| 4 |
</head> |
| 5 |
<body> |
| 6 |
|
| 7 |
<h1>OUTDATED</h1> |
| 8 |
|
| 9 |
<h2>Abstract:</h2> |
| 10 |
|
| 11 |
<p>This document covers all issues about ClanLib safely and correctly</p> |
| 12 |
|
| 13 |
<h3>Creating destroying objects</h3> |
| 14 |
|
| 15 |
<p>All objects must both created AND destroyed inside their respective ::init() and ::deinit() groups. This means that letting an object fall out of scope might not work, especially in CL_Application::main().<p> |
| 16 |
|
| 17 |
<code> |
| 18 |
... |
| 19 |
Do_Bar() |
| 20 |
{ |
| 21 |
CL_SetupSound::init(); |
| 22 |
CL_SoundBuffer *soundbuffer = CL_Sample::create("beep.wav",NULL); |
| 23 |
CL_SoundBuffer_Session ses = soundbuffer->play(); |
| 24 |
... |
| 25 |
|
| 26 |
delete soundbuffer; // Good |
| 27 |
|
| 28 |
CL_SetupSound::deinit(); //All user objects must be destroyed before now |
| 29 |
} //ses destroyed here, very bad |
| 30 |
|
| 31 |
</code> |
| 32 |
|
| 33 |
<p> In this code ses is destroyed when it goes out of scope, which is after it should be destroyed. A better solution to this problem would be either wrap Do_Bar in some other function. eg<p> |
| 34 |
|
| 35 |
<code> |
| 36 |
|
| 37 |
init_sound() { |
| 38 |
CL_SetupSound::init(); |
| 39 |
Do_Bar(); |
| 40 |
CL_SetupSound::deinit(); |
| 41 |
} |
| 42 |
|
| 43 |
Do_Bar() { |
| 44 |
CL_SoundBuffer *soundbuffer = CL_Sample::create("beep.wav",NULL); |
| 45 |
CL_SoundBuffer_Session ses = soundbuffer->play(); //Ok here |
| 46 |
... |
| 47 |
|
| 48 |
delete soundbuffer; // Good |
| 49 |
} |
| 50 |
</code> |
| 51 |
|
| 52 |
<p> The code above is perfectly safe as ses is destroy when Do_Bar goes out of scope which is before deinit(). Another way is to use pointers.</p> |
| 53 |
|
| 54 |
</body> |
| 55 |
</xml> |