# Weird error when loading 242 samples

**URL:** <https://forum.juce.com/t/weird-error-when-loading-242-samples/16591>\
**Category:** MacOSX and iOS\
**Created:** [February 6, 2016, 4:27pm UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591 "2016-02-06T16:27:10Z")\
**Posts on this page:** 9\
**Page:** 2

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [February 8, 2016, 6:41pm UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591/21 "2016-02-08T18:41:23Z")

</div>

Thanks, that makes sense.

Jules can you confirm this is a bug on the JUCE side?&nbsp;

And about the other question (how to use readers): I have two possible ways of implementation:

1. Create a reader (as ScopedPointer member variable)&nbsp;when the sample is loaded (the reader&nbsp;is needed to prebuffer the sample and waits until the background thread requests more data). This is the current implementation and it means that the reader will keep the file handle open until the sample is unloaded.

2. Create a reader when the sample is loaded (as local variable), fill the prebuffer and delete it. If the sample is started, create a new reader and start fetching data in the background thread (and keep the reader alive until the sample is stopped so it doesn't have to create a new object every 8192 samples). There is enough time to create the reader because we have the prebuffer but it imposes a little bit of overhead at the sample start time (for the background thread, not the audio thread). &nbsp;On the other hand it reduces the open file handles to the smallest possible amount. So after all this is a tradeoff between having open file handles and less overhead at voice starts and this is where I would love to hear some pros / cons.

BTW Reaper also uses open file handles for their sample management.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 9, 2016, 10:39am UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591/22 "2016-02-09T10:39:34Z")

</div>

Wouldn't call this a "bug", it's just how unix filesystems work..&nbsp;

However looking at otristan's&nbsp;code for setting the rlimit, it does look like a&nbsp;better plan than our current "try setting it to infinity but don't worry if it doesn't succeed" approach. I'll try out something like that.

The number that you need to keep open depends on many things... Certainly for short samples you'd be better off just loading them all into memory, and only keep really long ones&nbsp;as memory-maps.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [February 9, 2016, 11:32am UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591/23 "2016-02-09T11:32:27Z")

</div>

FYI see my commit

&nbsp;https://github.com/julianstorer/JUCE/commit/fbda96b899c15b9fd44c947ac4aa2b6948f51cd7

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [February 9, 2016, 11:40am UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591/24 "2016-02-09T11:40:25Z")

</div>

I do 2) in my code.

I have soundbank with 40k samples so 1° is not even possible even if you raise the number of possible file opened.

---

<div class="post-metadata">

**Author:** ![chrisboy2000](https://avatars.discourse-cdn.com/v4/letter/c/a3d4f5/32.png) [@chrisboy2000](https://forum.juce.com/u/chrisboy2000)\
**Post date:** [February 9, 2016, 11:02pm UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591/25 "2016-02-09T23:02:00Z")

</div>

Why is it not possible? The max limit (2^63-1)&nbsp;is way above that number or am I missing the point?

BTW I&nbsp;can already bypass the streaming engine for small samples but for now I still keep the handles open, so I will change that.

---

<div class="post-metadata">

**Author:** ![otristan](https://avatars.discourse-cdn.com/v4/letter/o/f0a364/32.png) [@otristan](https://forum.juce.com/u/otristan)\
**Post date:** [February 10, 2016, 9:13am UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591/26 "2016-02-10T09:13:34Z")

</div>

You cannot open that many files on OSX AFAIK even when raising the limit.

There is the same kind of issue on Windows

from stackoverflow

Win32 API calls (CreateFile(), WriteFile(), and so forth) and the default maximum number of open files has been increased to 16384. The maximum can be increased further by using the --max-open-files=N option at server startup.

except you cannot control the&nbsp;server startup :)

&nbsp;

&nbsp;

---

<div class="post-metadata">

**Author:** ![gphofa](https://avatars.discourse-cdn.com/v4/letter/g/3ec8ea/32.png) [@gphofa](https://forum.juce.com/u/gphofa)\
**Post date:** [May 3, 2018, 12:15pm UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591/27 "2018-05-03T12:15:52Z")

</div>

Hi!

I am not sure if I do not undestand the purpose of this fix or it does not work as intended here.  
As far as I understand, MaxNumFileHandlesInitialiser() should increase the maximum number of files to 8192 (or something below if it is not possible to set it to that value).

Here the initial call to getrlimit(RLIMIT\_NOFILE, &lim) will always return the system default value lim.rlim\_cur=256. So the first call setMaxNumberOfFileHandles(0) will just return true, because 256\>0.  
So the limit will remain on 256 and is never increased.

Am I doing something wrong?

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [May 3, 2018, 2:07pm UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591/28 "2018-05-03T14:07:13Z")

</div>

Hmm, that does seem to be a mistake. I guess nobody else ever needed this function in the last 2 years!  
I’ll push a fix to develop right away, thanks!

---

<div class="post-metadata">

**Author:** ![gphofa](https://avatars.discourse-cdn.com/v4/letter/g/3ec8ea/32.png) [@gphofa](https://forum.juce.com/u/gphofa)\
**Post date:** [May 3, 2018, 3:18pm UTC](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591/29 "2018-05-03T15:18:57Z")

</div>

Thanks for the quick fix!

[Previous page](https://forum.juce.com/t/weird-error-when-loading-242-samples/16591.md?page=1)
