1

I am developing an Android based project that requires some components to run via the NDK because I'm using code already written in C. I am having a very weird problem where the program crashes when calling a specific function:

numin_read(ns,values+data_spec->N_inputs*i+j0);

ns is a pointer to a struct and values is a pointer to a double array, the rest is just an integer offset, all of which are valid (ns has been used already by the time this function is called and the malloc call is checked for a NULL-pointer). The function is located in another *.c file (numin.c) and the declaration of it (in numin.h) is void numin_read (numin_source *, double *);

It isn't even the first time the program calls a function from numin.c but the only one that crashes. I know it's not something in the function that's causing the crash because it still does it if I return in the first line.

I'm struggling to get the debugger to attach to the program (despite doing everything described here) so I really have no idea what's going on in there. Anybody have any idea why this isn't working? I've used this code on Linux and it works fine.

Sorry if there's very little to go on, will keep trying to get the debugger to attach so I can give more details. Please let me know what other info you need.

Edit: I don't know if this makes a difference but in order to build the program, I compile each of the individual c files into a series of object files (using the NDK standalone toolchain), archive these object files into an ar archive and then call upon this as a PREBUILT_STATIC_LIBRARY when making the main c file into the shared library that I call via the NDK...

Edit: This is beyond weird. I changed the definition of numin_read to void numin_read() and commented out all the code except for a single return statement. Essentially I'm calling a function that takes in no parameters and just returns... and it still crashes! I did manage to get something out of LogCat though: signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 00000004 As far as I understand it has to do with memory allocation? But I'm not malloc-ing, I'm calling a just function... What am I missing?

Community
  • 1
  • 1
JonoCoetzee
  • 969
  • 2
  • 15
  • 30
  • Increase the warning level of your compiler, and **mind the warnings**. – pmg Oct 10 '11 at 15:00
  • @pmg Neither the calling file nor the numin.c file have warnings in the either of the responsible functions. I compiled with -Wall -Wextra... – JonoCoetzee Oct 10 '11 at 15:08

2 Answers2

0

The expression

values+data_spec->N_inputs*i+j0

is the same as

values[data_spec->N_inputs * i + j0]

I suspect you try to access an element you don't really have access to ... or maybe an uninitialized element.

pmg
  • 106,608
  • 13
  • 126
  • 198
  • For the first iteration i is 0, this means that everything simplifies to just values... I will double check that I can access the array... – JonoCoetzee Oct 10 '11 at 16:17
  • values is allocated correctly, I wrote to it and then printed out the contents, but the this just serves a pointer so that values read from the file in the function are stored in the array. The function is called once for every line in the file, hence the need for the offset. Nevertheless it still crashes when I pass nothing into it and just return... I'm not expert in C but I've never come across something like this before... – JonoCoetzee Oct 10 '11 at 16:42
  • You are definitely invoking Undefined Behavior somewhere in your code, and possibly messing up the stack. Beware **all** casts, make sure you have the correct prototype in scope for all functions, not just `numin_read`! – pmg Oct 10 '11 at 17:00
  • Thanks, will look into that. Would it be something that shows up with -Wall -Wextra? Cuz I recompiled everything with those flags and the most I got was a series of "comparison between signed and unsigned integer expressions" and "suggest parentheses around '&&' within '||'". It would help if this was my code however... – JonoCoetzee Oct 10 '11 at 17:18
  • With `gcc` add `-Wmissing-prototypes`. Also try `-std=c89 -pedantic` or `-std=c99 -pedantic` if the code doesn't need gnu specific extensions. – pmg Oct 10 '11 at 17:22
  • Unfortunately those flags didn't pick anything up... and as far as I can see, this code doesn't cast... thanks though... – JonoCoetzee Oct 10 '11 at 18:43
0

Not my brightest moment ever, the .h files that I was using to build the final NDK shared library were not symlinks to the original .h files that I was using to build the object files. This meant that any changes I made to those weren't being propagated through... still have to check that everything's now fine but sorry for wasting your time...

JonoCoetzee
  • 969
  • 2
  • 15
  • 30
  • It's working now, this answer is correct, but the program kept crashing because there was an error in the file the function was reading. Thanks for the help! – JonoCoetzee Oct 11 '11 at 10:47