0

I am getting a null pointer dereference in Eclipse and I don't know how to acctually fix this bug.

Here is the log error:

04-18 10:27:48.538: A/DEBUG(22481): Build fingerprint: 
'OnePlus/OnePlus3/OnePlus3T:8.0.0/OPR6.170623.013/12041042:user/release- keys'
04-18 10:27:48.538: A/DEBUG(22481): Revision: '0'
04-18 10:27:48.538: A/DEBUG(22481): ABI: 'arm'
04-18 10:27:48.538: A/DEBUG(22481): pid: 22024, tid: 22078, name: GLThread     8317  >>>  <<<
04-18 10:27:48.538: A/DEBUG(22481): signal 11 (SIGSEGV), code 1     (SEGV_MAPERR), fault addr 0x0
04-18 10:27:48.538: A/DEBUG(22481): Cause: null pointer dereference
04-18 10:27:48.538: A/DEBUG(22481):     r0 b9bb0e74  r1 00000000  r2     00000001  r3 00000000
04-18 10:27:48.538: A/DEBUG(22481):     r4 000aa185  r5 afdd0c28  r6     b9bb1a40  r7 b9b83a40
04-18 10:27:48.538: A/DEBUG(22481):     r8 0000cf00  r9 000ac440  sl     00000000  fp 0000ac44
04-18 10:27:48.538: A/DEBUG(22481):     ip 000bdcc0  sp c1bfe1b0  lr     c25fd079  pc c25fd084  cpsr 08070030
04-18 10:27:48.541: A/DEBUG(22481): backtrace:
04-18 10:27:48.541: A/DEBUG(22481):     #00 pc 0022d084      /data/app/com.-xBYgyq_62u2R3qWK0qBgkQ==/lib/arm/libgame.so     (_ZN12CSoundSource12GetAudioDataEPfii+87)

And the error is occuring in this part of the code:

void CSoundSource::GetAudioData(float *data, int frameStart, int frames)
{
int i=0;
while(i<frames)
    {
    int temp_samp = (frameStart+i) % TotalSamples;
    int tmp_div = temp_samp / blockSize;
    int tmp_mod = temp_samp % blockSize;
        *data = block[tmp_div]->buffer[tmp_mod][0] * 0.00003f;
    data++;
        *data = block[tmp_div]->buffer[tmp_mod][1] * 0.00003f;
    data++;
    i++;
    }
}

Could someone tell me how to fix the null dereferencing? Or give a hint to point me in the right direction?

Thank you.

Dan Golea
  • 41
  • 1
  • 6
  • What is the value of `data`? Where does `block` come from? Please construct a [minimal test case](https://stackoverflow.com/help/mcve). – Oliver Charlesworth Apr 18 '18 at 07:44
  • 1
    You have a pointer which is null? You dereference multiple pointers in the code you show, any of them could be null. Use a *debugger* to catch the crash as it happens, locate the exact line where it happens, and see which of your pointers are null. – Some programmer dude Apr 18 '18 at 07:44
  • Leave a link to the original code. Better yet, use shared pointers and iterators instead. OR use a Try-Catch-Throw block. – Aniket Chowdhury Apr 18 '18 at 07:51
  • @AniketChowdhury Do not ask for a link to original code. Ask for a [mcve]. We do not want links to (potentially giant) code dumps, we want what helps with helping OP. And making an MCVE has helped countless askers to suddenly spot the error themselves. In this case for example, OP has already completely focused unhelpfully on the function which only shows the symptoms. The source of the error is outside of the shown code. But that "outside" is possibly large. We just want the part which has the error, ideally. – Yunnosch Apr 18 '18 at 07:53
  • float *data = new float[size*DEFChannels]; And block seems to not be null from what I've seen. I wish I could use a debugger really. It's an old project with half of the code ported from Visual Studio into Eclipse Android and I'm the first person to touch it in the last 4 years, the old dev team is gone and from what I've seen from the old documentation the only way to debug it on Android is to just print strings of the variables. On Windows the app is running fine. – Dan Golea Apr 18 '18 at 07:54
  • 1
    https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ https://stackoverflow.com/questions/2069367/how-to-debug-using-gdb https://ericlippert.com/2014/03/21/find-a-simpler-problem/ If you manage to really do those three methods and do not see your error I pay your dinner, provided you come see me. ;-) – Yunnosch Apr 18 '18 at 07:55
  • Remove the lines with `*data=` and the problem will disappear. –  Apr 18 '18 at 08:13
  • @Yunnosch thanks i found how to solve the crash :). Also thanks everyone for the helping! – Dan Golea Apr 18 '18 at 09:24
  • @Dan Golea Please don't forget to post the error you found and the solution as an answer (you can answer your own question) - even if people won't have the exactly same problem, someone with a similar issu might run across your question and get an idea where to start looking. – CharonX Apr 19 '18 at 11:47
  • @CharonX thank you. – Dan Golea Apr 19 '18 at 12:15

1 Answers1

2

I found the problem.

Apparently the crash was caused because of block[tmp_div] being NULL. A different thread wasn't allocating a value to it before *data = block[tmp_div]->buffer[tmp_mod][0] * 0.00003f; so I had to check every block[] to see if it's NULL and if so to try to give it the correct value again.

Thus the part of the code i posted looks like this:

void CSoundSource::GetAudioData(float *data, int frameStart, int frames)
{
    int i=0;
    while(i<frames)
    {
        int temp_samp = (frameStart+i) % TotalSamples;
        int tmp_div = temp_samp / blockSize;
        int tmp_mod = temp_samp % blockSize;
        if(block[tmp_div])
            *data = block[tmp_div]->buffer[tmp_mod][0] * 0.00003f;
        data++;
        if(block[tmp_div])
            *data = block[tmp_div]->buffer[tmp_mod][1] * 0.00003f;
        data++;
        i++;
    }
}
Dan Golea
  • 41
  • 1
  • 6
  • 1
    Multithreading can be rather tricky... Don't forget to use [Mutexes](http://www.cplusplus.com/reference/mutex/mutex/) to ensure only one thread reads or writes a shared resources at a time. – CharonX Apr 19 '18 at 12:50