问题
I have recently encountered a really weird problem while debugging C/C++ application in Android Studio.
I am using Android Studio 3.0
Here is my JNI Call
JNIEXPORT jshortArray JNICALL
Java_net_crosp_android_library_ecgaudioprocessor_EcgSignalProcessorDemodulator_demodulate(
JNIEnv *env, jobject instance, jshortArray readBuffer_) {
/* get size of the array */
jsize len = env->GetArrayLength(readBuffer_);
int sample_rate = 44100;
int kd = 12;
SignalConfiguration signal_cfg = {sample_rate, kd, 1700, sample_rate, sample_rate / kd, 200};
ECGFMDemodulator *ecg_fm_demodulator = new ECGFMDemodulator();
ecg_fm_demodulator->set_cfg(signal_cfg);
ECGFMDemodulator *ecg_fm_demodulator2 = new ECGFMDemodulator(signal_cfg);
/* get the body of array; it will be referecende by C pointer */
jshort *body = env->GetShortArrayElements(readBuffer_, JNI_FALSE);
env->ReleaseShortArrayElements(readBuffer_, body, JNI_COMMIT);
return readBuffer_;
}
Class ECGFMDemodulator
class ECGFMDemodulator {
private:
SignalConfiguration m_signal_configuration_;
public:
ECGFMDemodulator(SignalConfiguration configuration);
void set_cfg(SignalConfiguration param);
};
And implementation
ECGFMDemodulator::ECGFMDemodulator(SignalConfiguration configuration) {
this->m_signal_configuration_ = configuration;
std::cout.rdbuf(new Androidbuf);
// Print values
std::cout << "------ VALUES OF CONFIGURATION --------" << std::endl;
std::cout << "Sample rate : " << this->m_signal_configuration_.sample_rate << std::endl;
std::cout << "KD : " << this->m_signal_configuration_.kd << std::endl;
std::cout << "FC : " << this->m_signal_configuration_.fc << std::endl;
std::cout << "FS : " << this->m_signal_configuration_.fs << std::endl;
std::cout << "FS2 : " << this->m_signal_configuration_.fs2 << std::endl;
delete std::cout.rdbuf(0);
this->init_filters();
}
void ECGFMDemodulator::set_cfg(SignalConfiguration param) {
this->m_signal_configuration_ = param;
}
I am creating an object like that
int sample_rate = 44100;
int kd = 12;
SignalConfiguration signal_cfg = {sample_rate, kd, 1700, sample_rate, sample_rate / kd, 200};
ECGFMDemodulator *ecg_fm_demodulator = new ECGFMDemodulator();
ecg_fm_demodulator->set_cfg(signal_cfg);
ECGFMDemodulator *ecg_fm_demodulator2 = new ECGFMDemodulator(signal_cfg);
And while debugging I got interesting results
- Creating
signal_cfg
, as you can see values are valid
- Watching values in the constructor, as you can see values are random
- After creating this object, values are correct and printed to console with valid values. However in debugger they are still garbage.
I/std: ------ VALUES OF CONFIGURATION -------- I/std: Sample rate : 44100 I/std: KD : 12 I/std: FC : 1700 I/std: FS : 44100 I/std: FS2 : 3675
I have also tried to use lldb
but got the same values, that are not valid. I've also tried to pass by reference, but got the same result.
(lldb) frame variable
(ECGFMDemodulator *) this = 0xb7688d29
(SignalConfiguration) param = (sample_rate = -1217884423, kd = -1204349992, fc = -1217887850, fs = -1982518100, fs2 = -1983635321, delay = 36)
What is wrong with debugger, I am not able to debug my program, because I am not sure that values being displayed are valid.
Does anyone experiencing similar problems ?
I would be grateful for any help.
Thanks
来源:https://stackoverflow.com/questions/47389139/bug-in-android-c-c-debugger-incorrect-argument-values