I work with VS2012, on a project which combines C and C++ code. This problem does not happen when running the code in GCC.
(See screenshot at the bottom)
The below C function fails on stack overflow, immediately when the function FeedForwardNetwork_new_from_string()
is called. The debugger breaks after the call to that function, but before any of that function's code is being executed.
FeedForwardNetwork *FeedForwardNetwork_new_from_file(const char *filePath) {
char *file_contents;
long input_file_size;
FILE *input_file = fopen(filePath, "rb");
FeedForwardNetwork *ffn;
if (input_file == NULL) {
return NULL;
}
fseek(input_file, 0, SEEK_END);
input_file_size = ftell(input_file);
rewind(input_file);
file_contents = (char*)malloc((input_file_size + 1) * (sizeof(char)));
fread(file_contents, sizeof(char), input_file_size, input_file);
file_contents[input_file_size] = '\0';
fclose(input_file);
ffn = FeedForwardNetwork_new_from_string(file_contents);
free(file_contents);
return ffn;
}
This is the flow to the function, starting from main()
:
int main(int argc, char **argv) {
VideoAnalyzerUtils::getNeuralNetwork();
return 0;
}
The function getNeuralNetwork()
, which calls the problematic function:
FeedForwardNetwork *VideoAnalyzerUtils::getNeuralNetwork() {
if (VideoAnalyzerUtils::network_ == NULL) {
VideoAnalyzerUtils::network_ = FeedForwardNetwork_new_from_file("net.txt");
}
return VideoAnalyzerUtils::network_;
}
The implementation of FeedForwardNetworkSettings_new_from_string()
:
FeedForwardNetworkSettings *FeedForwardNetworkSettings_new_from_string(const char *settingsStr) {
char buffer[NEURON_STR_READ_BUFFER_SIZE];
int withBias;
double trainingRate ;
double outputLayerTrainingRate;
int hiddenLayerType;
int outputLayerType;
FeedForwardNetworkSettings *settings;
_Neuron_getValueFromString(settingsStr, "withBias=", buffer);
if (strcmp(buffer, "") == 0) {
return NULL;
}
withBias = atoi(buffer);
_Neuron_getValueFromString(settingsStr, "trainingRate=", buffer);
if (strcmp(buffer, "") == 0) {
return NULL;
}
trainingRate = atof(buffer);
_Neuron_getValueFromString(settingsStr, "outputLayerTrainingRate=", buffer);
if (strcmp(buffer, "") == 0) {
return NULL;
}
outputLayerTrainingRate = atof(buffer);
_Neuron_getValueFromString(settingsStr, "hiddenLayerType=", buffer);
if (strcmp(buffer, "") == 0) {
return NULL;
}
hiddenLayerType = atoi(buffer);
_Neuron_getValueFromString(settingsStr, "outputLayerType=", buffer);
if (strcmp(buffer, "") == 0) {
return NULL;
}
outputLayerType = atoi(buffer);
settings = (FeedForwardNetworkSettings*)malloc(sizeof(FeedForwardNetworkSettings));
settings->withBias_ = withBias;
settings->trainingRate_ = trainingRate;
settings->outputLayerTrainingRate_ = outputLayerTrainingRate;
settings->hiddenLayerType_ = (NeuronActivationFunction)hiddenLayerType;
settings->outputLayerType_ = (NeuronActivationFunction)outputLayerType;
return settings;
}
Can you please help me to understand why it would happen and how to fix it?
A screenshot of the error (Please use browser's zoom-in to see it better):