<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>switching stream files</title>
		<link>http://www.allegro.cc/forums/view/612963</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Fri, 19 Jul 2013 21:13:36 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>This is probably straight forward, but no matter what I do I get crashes.</p><p>What I&#39;m doing is stream audio using <span class="source-code"><a href="http://www.allegro.cc/manual/al_load_audio_stream_f"><span class="a">al_load_audio_stream_f</span></a></span>, and I want to be able to switch the file it plays from, replacing the current one.</p><p>Here&#39;s what I tried. It causes the program to crash if I change to a file that was already playing:</p><div class="source-code snippet"><div class="inner"><pre>ALLEGRO_STREAM <span class="k3">*</span>stream <span class="k3">=</span> NULL<span class="k2">;</span> <span class="c">//defined elsewhere</span>

<span class="k1">void</span> streamAudio<span class="k2">(</span><a href="http://www.allegro.cc/manual/ALLEGRO_FILE"><span class="a">ALLEGRO_FILE</span></a> <span class="k3">*</span>file<span class="k2">)</span><span class="k2">{</span>
   <span class="k1">if</span><span class="k2">(</span>stream<span class="k2">)</span><span class="k2">{</span>
      <a href="http://www.allegro.cc/manual/al_detach_audio_stream"><span class="a">al_detach_audio_stream</span></a><span class="k2">(</span>stream<span class="k2">)</span><span class="k2">;</span>
   <span class="k2">}</span>
   stream <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_audio_stream_f"><span class="a">al_load_audio_stream_f</span></a><span class="k2">(</span>file, <span class="s">".ogg"</span>, <span class="n">2</span>, <span class="n">1024</span><span class="k2">)</span><span class="k2">;</span>
   <a href="http://www.allegro.cc/manual/al_set_audio_stream_playmode"><span class="a">al_set_audio_stream_playmode</span></a><span class="k2">(</span>stream,ALLEGRO_PLAYMODE_LOOP<span class="k2">)</span><span class="k2">;</span>
   <a href="http://www.allegro.cc/manual/al_attach_audio_stream_to_mixer"><span class="a">al_attach_audio_stream_to_mixer</span></a><span class="k2">(</span>stream, mixer<span class="k2">)</span><span class="k2">;</span>
<span class="k2">}</span>
</pre></div></div><p>

I feel like not calling <span class="source-code"><a href="http://www.allegro.cc/manual/al_destroy_audio_stream"><span class="a">al_destroy_audio_stream</span></a></span> would cause a memory leak, but calling it causes a crash when I exit. What am I doing wrong? <img src="http://www.allegro.cc/forums/smileys/huh.gif" alt="???" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Sat, 13 Jul 2013 21:33:25 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Maybe al_load_audio_stream_f returns NULL? You could check that, as well as mixer. And yes, you are leaking memory too. Make your program corect and then try to find out where the crashes are coming from.  </p><p>Asa nd isede, it it hard to drun the game in a debuggger? On Linux, I run my programs with the gdb debugger or valgrind the memory checker and I can often pinpoint the problem easily. Most difficult to  find remains memory corruption where valid data is overwitten by a pointer or a stack problem somewhere else.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Sat, 13 Jul 2013 23:22:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve looked into it, and it seems that <span class="source-code"><a href="http://www.allegro.cc/manual/al_load_audio_stream_f"><span class="a">al_load_audio_stream_f</span></a></span> closes the memfile. Can this be prevented?</p><p>EDIT: That was the problem. I changed my system to accommodate for the closing of the memfile after <span class="source-code"><a href="http://www.allegro.cc/manual/al_load_audio_stream_f"><span class="a">al_load_audio_stream_f</span></a></span> is called. I don&#39;t think this behaviour is in the manual, maybe it should be added.</p><p>EDIT 2: Is it possible that <span class="source-code"><a href="http://www.allegro.cc/manual/al_load_audio_stream_f"><span class="a">al_load_audio_stream_f</span></a></span> causes a memory leak since you can&#39;t call <span class="source-code"><a href="http://www.allegro.cc/manual/al_fclose"><span class="a">al_fclose</span></a></span> on the same file after its called? Or does it close the memfile?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Sun, 14 Jul 2013 10:50:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Yeah, I don&#39;t think its desired behavior for _f functions to close the file. I&#39;d call that a bug.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 14 Jul 2013 10:52:46 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It depends. If it needs to hold onto the file, it has no choice. I think that&#39;s the case here. It&#39;s the same with load_ttf_f. TTFs have to be kept open and so do streams, otherwise you might as well load them into memory.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 14 Jul 2013 11:20:51 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Why would it need to close the file handle you handed to it? The main purpose behind the _f functions is that you already have a handle open, and in some/many cases, its multiple actual files in a single file, so you&#39;re going to need to have that handle stay open across calls to _f functions.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 14 Jul 2013 11:25:36 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Think about this for a second. The main reason for the _f functions is mainly that you can use your own file access routines so you can have a file embedded in a whatever and Allegro can still use it. When you pass a file handle to a load_stream function, it has to keep that file open, seek around in it. When it&#39;s done, you may not know about it. So it makes the most sense for it to keep it itself. If you need another one, then open it again. Not all of the _f functions work like this, just the ones that need to. EDIT: And it&#39;s documented in the manual for at least some of them.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Trent Gamblin)</author>
		<pubDate>Sun, 14 Jul 2013 11:34:26 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I dunno, I still don&#39;t think it should &quot;own&quot; your handle. You should know when you stop playing your own files. But, if there&#39;s no other way, I suppose the only solution is to always hand them file slices.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Sun, 14 Jul 2013 12:10:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Just throwing out an idea , but if it is the case hat it closes the file behind your back, a callback function or something similar would be useful to notify the rest of the program. </p><p>Anyway, the documentation needs to be updated for <span class="source-code"><a href="http://www.allegro.cc/manual/al_load_audio_stream_f"><span class="a">al_load_audio_stream_f</span></a></span> so it&#39;s ore clear what is going on.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Sun, 14 Jul 2013 16:19:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Does it have to close a memfile? After all the data <i>is</i> in the memory.<br />Also, this is what I do now. Could this cause a memory leak?</p><div class="source-code"><div class="toolbar"><span class="button numbers"><b>#</b></span><span class="button select">Select</span><span class="button expand">Expand</span></div><div class="inner"><span class="number">  1</span><span class="c">//these are defined elsewhere</span>
<span class="number">  2</span><a href="http://www.allegro.cc/manual/ALLEGRO_FILE"><span class="a">ALLEGRO_FILE</span></a> <span class="k3">*</span>file <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number">  3</span><a href="http://www.allegro.cc/manual/ALLEGRO_AUDIO_STREAM"><span class="a">ALLEGRO_AUDIO_STREAM</span></a> <span class="k3">*</span>stream <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number">  4</span>
<span class="number">  5</span><span class="c">//following code called when starting a stream</span>
<span class="number">  6</span><span class="k1">if</span><span class="k2">(</span>stream<span class="k2">)</span><span class="k2">{</span>
<span class="number">  7</span>  <a href="http://www.allegro.cc/manual/al_destroy_audio_stream"><span class="a">al_destroy_audio_stream</span></a><span class="k2">(</span>stream<span class="k2">)</span><span class="k2">;</span>
<span class="number">  8</span><span class="k2">}</span>
<span class="number">  9</span><span class="c">//this sets file to al_open_memfile</span>
<span class="number"> 10</span>openMemFile<span class="k2">(</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 11</span><span class="c">//I assume this calls al_fclose on file</span>
<span class="number"> 12</span>stream <span class="k3">=</span> <a href="http://www.allegro.cc/manual/al_load_audio_stream_f"><span class="a">al_load_audio_stream_f</span></a><span class="k2">(</span>file, <span class="s">".ogg"</span>, <span class="n">2</span>, <span class="n">1024</span><span class="k2">)</span><span class="k2">;</span>
<span class="number"> 13</span><span class="c">//file is now a bad pointer?</span>
<span class="number"> 14</span>file <span class="k3">=</span> NULL<span class="k2">;</span>
<span class="number"> 15</span><a href="http://www.allegro.cc/manual/al_set_audio_stream_playmode"><span class="a">al_set_audio_stream_playmode</span></a><span class="k2">(</span>stream,ALLEGRO_PLAYMODE_LOOP<span class="k2">)</span><span class="k2">;</span>
<span class="number"> 16</span><a href="http://www.allegro.cc/manual/al_attach_audio_stream_to_mixer"><span class="a">al_attach_audio_stream_to_mixer</span></a><span class="k2">(</span>stream, mixer<span class="k2">)</span><span class="k2">;</span>
</div></div><p>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Sun, 14 Jul 2013 19:53:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Well from this, it seems you have to close the memfile manually when you are done with it. Probably allegro allocates it&#39;s own memory amlso for the memfile struct that also needs to be freed. ou need to free the memory you alocated yourself, normally...</p><p><a href="https://www.allegro.cc/manual/5/al_open_memfile">https://www.allegro.cc/manual/5/al_open_memfile</a>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Mon, 15 Jul 2013 21:17:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>To allegro functions, a memfile isn&#39;t special. It treats them the same as a &quot;normal&quot; file. If a _f function is going to call al_close_file() on one, it&#39;ll call it on the other.</p><p>So at the very least the ALLEGRO_FILE gets freed, but you&#39;ll want to free up the memory you allocated for the memfile to use.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Thomas Fjellstrom)</author>
		<pubDate>Mon, 15 Jul 2013 21:26:19 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Sorry, I forgot to mention that the memory the file is pointing to is defined in a header so it can&#39;t be freed. The memory leak I was concerned about is the lack of <span class="source-code"><a href="http://www.allegro.cc/manual/al_fclose"><span class="a">al_fclose</span></a></span>
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Mon, 15 Jul 2013 22:44:22 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I looked in the memfile.c sources, and yes, you WILL leak a whole ALLEGRO_FILE_MEMFILE struct that gets al_malloc() ed on opening the file. So you should really call al_fclose at the right time. Why not keep the file pointer around? You don&#39;t need to free your own buffer itf it&#39;s statical, and memfile won&#39;t free your buffer either, but you have to free the memory that gets allocated for the ALLEGRO_FILE_MEMFILE itself.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Tue, 16 Jul 2013 20:17:13 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;m not sure that&#39;s the case. Because the pointer to the ALLEGRO_FILE becomes bad after calling <span class="source-code"><a href="http://www.allegro.cc/manual/al_load_audio_stream_f"><span class="a">al_load_audio_stream_f</span></a></span>, that means <span class="source-code"><a href="http://www.allegro.cc/manual/al_load_audio_stream_f"><span class="a">al_load_audio_stream_f</span></a></span> must call al_fclose at some point, right?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Wed, 17 Jul 2013 04:10:15 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I checked the sources (in the GIT version of Allegro) of the acodec and audio plugins more, and from what I see</p><p>1) al_load_audio_stream DOES call al_flcose on the file it opens itself.<br />2) However, al_load_audio_stream_f does NOT call al_flcose on the file you pass to it.<br />3) However however, al_destroy_audio_stream should close that file for you, since it calls al_fclose. </p><p>So I&#39;d think that you have to call al_close_audio_stream on the stream pointer, and then the file will also be deallocated. I don&#39;t know what will happen with the memfile&#39;s own buffer, though, but I suppose it will be unaffected.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Wed, 17 Jul 2013 04:49:04 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>after calling al_load_audio_stream_f, the file pointer becomes bad, meaning its pointing to free memory (which means al_load_audio_stream_f must have freed it). al_destroy_audio_sample does not close the file or else the program would crash.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (ph03nix)</author>
		<pubDate>Fri, 19 Jul 2013 03:42:10 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Hmmm, and are you using the same version of Allegro or an older one? I don&#39;t know how what you describe could happen from the source code I saw.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (beoran)</author>
		<pubDate>Fri, 19 Jul 2013 21:13:36 +0000</pubDate>
	</item>
</rss>
