# the likely bug with Optix3.0.1

**URL:** <https://forums.developer.nvidia.com/t/the-likely-bug-with-optix3-0-1/33464>\
**Category:** OptiX\
**Created:** [June 1, 2014, 1:18pm UTC](https://forums.developer.nvidia.com/t/the-likely-bug-with-optix3-0-1/33464 "2014-06-01T13:18:10Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![maxli](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@maxli](https://forums.developer.nvidia.com/u/maxli)\
**Post date:** [June 1, 2014, 1:18pm UTC](https://forums.developer.nvidia.com/t/the-likely-bug-with-optix3-0-1/33464/1 "2014-06-01T13:18:10Z")

</div>

I have developped application programs with Optix3.0.1 for almost one year, however, recently I met a problem.  
My application program is quite complicated, so I won’t talk about it, but I will depict my problem related my program briefly . Now let’s begin, it is quite an easy task.  
I want to pass an integer into the ray\_generation program, then in the ray\_generation program, memories will be allocated with the passed number,  
at last, I would like to initialize the memeories with values. But it can’t work normally, the debug information is that:

```auto
Unhandled exception at 0x0fbe9936 in NIMCAsst.exe: 0xC0000005: Access violation reading location 0x00000014."

```

The codes contributing to the exception is as follow.  
In the Main funciton,I declare a variable

```auto
context["number"]->setUint(2);

```

And in the xx.cu file

```auto
rtDeclareVariable(uint,number,,);

```

Then in the ray\_generation program

```auto
unsigned int * ab =new unsigned int[number];
ab[0] = 1;

```

After some experiments, I found the critical code is “ab[0]=1”,if I delete the “ab[0]=1”,it works normally, otherwise it will pop up an exception given above,  
I’m not quite sure what’s wrong with it, is there someone can give me a suggestion. How can I fix the problem? Thank you for your answers

---

<div class="post-metadata">

**Author:** ![maxli](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@maxli](https://forums.developer.nvidia.com/u/maxli)\
**Post date:** [June 1, 2014, 1:29pm UTC](https://forums.developer.nvidia.com/t/the-likely-bug-with-optix3-0-1/33464/2 "2014-06-01T13:29:11Z")

</div>

Supplement: Operate System :Windows7  
development tool: Vs2010  
GPU : GTX650

---

<div class="post-metadata">

**Author:** ![droettger](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/droettger/32/14043_2.png) [@droettger](https://forums.developer.nvidia.com/u/droettger)\
**Post date:** [June 2, 2014, 7:08am UTC](https://forums.developer.nvidia.com/t/the-likely-bug-with-optix3-0-1/33464/3 "2014-06-02T07:08:44Z")

</div>

Dynamic memory allocations inside OptiX CUDA programs are not supported.

See OptiX Programming Guide Chapter 12 Caveats:  
“Use of the CUDA malloc, free, and printf functions within a program is not supported.”

Means the problem is the “new”. As long as you won’t access the data the compiler will eliminate it as dead code.

Since you know the amount of memory beforehand, because you control it with the number value, you could allocate your whole memory by creating a single output buffer (or input\_output buffer if you also read it) with number \* launchDim.x \* launchDim.y \* sizeof(unsigned int) size on the host resp. change the size of that buffer according to number and then calculate the proper pointer \*ab per launchIndex inside your ray generation program and continue from there.

Try if resizing that buffer induces a recompile. If yes, that would be a slow operation and depending on how often your number value changes, you could only grow the buffer and avoid shrinking it as a performance optimization.

---

<div class="post-metadata">

**Author:** ![maxli](https://developer.download.nvidia.com/images/forums/profile-default-devtalk-84.png) [@maxli](https://forums.developer.nvidia.com/u/maxli)\
**Post date:** [June 3, 2014, 7:07am UTC](https://forums.developer.nvidia.com/t/the-likely-bug-with-optix3-0-1/33464/4 "2014-06-03T07:07:15Z")

</div>

Thank you for answering the question.  
Actually,the value of “number” is unknown in the host code, it is calculated in the device code.Then  
memory corresponding to the valude will be allocated, in this way, it’s not a good choice to define a  
buffer.  
If optix don’t have access to dynamic memory allocation,I have to define an array with const number which  
is far more larger than the value of “number”, unfortunately, which will create extra memory unused. It  
sounds like a bad idea when taking the limited memory size of the device into consideration.To be frank, it is really  
a bad idea that the optix can’t handle dynamic memory allocation.

```auto
unsigned int ab[100];// 100 is larger than number to ensure

```

---

<div class="post-metadata">

**Author:** ![TomNVIDIA](https://sea2.discourse-cdn.com/nvidia/user_avatar/forums.developer.nvidia.com/tomnvidia/32/14181_2.png) [@TomNVIDIA](https://forums.developer.nvidia.com/u/TomNVIDIA)\
**Post date:** [June 14, 2022, 9:11pm UTC](https://forums.developer.nvidia.com/t/the-likely-bug-with-optix3-0-1/33464/5 "2022-06-14T21:11:55Z")

</div>

Closed on June 14, 2022, 9:11pm UTC


