Memory Leaks and more

This commit is contained in:
Frederik Beimgraben 2025-01-17 19:04:55 +01:00
parent 115f1ef588
commit 4ac8f2d4be
3 changed files with 28 additions and 33 deletions

View File

@ -227,25 +227,30 @@ void rescaleDoubleArray(
return; return;
} }
// First find the old minimum // Rescale the array
double dOldMin = pdIn[0]; for (int i = 0; i < iLen; i++) {
pdOut[i] = pdIn[i] * dFactor;
}
// First find the minimum
double dOldMin = pdOut[0];
for (int i = 1; i < iLen; i++) { for (int i = 1; i < iLen; i++) {
if (pdIn[i] < dOldMin) if (pdOut[i] < dOldMin)
dOldMin = pdIn[i]; dOldMin = pdOut[i];
} }
// Calculate the offset // Calculate the offset
double dOffset = dMin - dOldMin; double dOffset = dMin - dOldMin;
// Move the values to the new minimum
for (int i = 0; i < iLen; i++) {
pdOut[i] += dOffset;
}
#ifdef _DEBUG #ifdef _DEBUG
_debug("rescaleDoubleArray: dOldMin = %lf", dOldMin); _debug("rescaleDoubleArray: dOldMin = %lf", dOldMin);
_debug("rescaleDoubleArray: dOffset = %lf", dOffset); _debug("rescaleDoubleArray: dOffset = %lf", dOffset);
#endif #endif
// Rescale the array
for (int i = 0; i < iLen; i++) {
pdOut[i] = pdIn[i] * dFactor + dOffset;
}
} }
/* /*
@ -280,12 +285,14 @@ double *generateSineValues(
*/ */
_error("*generateSineValues: iNumValues must be equal to or greater than 0!"); _error("*generateSineValues: iNumValues must be equal to or greater than 0!");
if (iNumValues == 0) if (iNumValues == 0) {
/* /*
* This would result in an empty array; Won't lead to a fatal error but * This would result in an empty array; Won't lead to a fatal error but
* is still invalid; Warn the user. * is still invalid; Warn the user.
*/ */
_warn("*generateSineValues: iNumValues is 0!"); _warn("*generateSineValues: iNumValues is 0!");
return NULL;
}
// Initialize the output array // Initialize the output array
double *sineArray = NULL; double *sineArray = NULL;
@ -550,7 +557,6 @@ MMSignal *createSignalFromFile(char *pcInName) {
MMSignal *signal = createSignal(); MMSignal *signal = createSignal();
signal->pdValues = NULL;
signal->iNumValues = readDoubleArray(pcInName, &signal->pdValues); signal->iNumValues = readDoubleArray(pcInName, &signal->pdValues);
if (signal->iNumValues == 0) if (signal->iNumValues == 0)
@ -707,14 +713,15 @@ double calculateMedian(double *pdIn, int iLen) {
* \frac{pdIn_{\frac{|pdIn|}{2}} + pdIn_{\frac{|pdIn|}{2}+1}}{2} & \text{if } |pdIn| \text{ is odd} * \frac{pdIn_{\frac{|pdIn|}{2}} + pdIn_{\frac{|pdIn|}{2}+1}}{2} & \text{if } |pdIn| \text{ is odd}
* \end{cases} \] * \end{cases} \]
*/ */
double *pdInSorted = NULL;
pdInSorted = (double*) malloc(sizeof(double) *iLen);
if (iLen == 0) { if (iLen == 0) {
_warn("calculateMedian: iLen is 0"); _warn("calculateMedian: iLen is 0");
return 0; return 0;
} }
double *pdInSorted = NULL;
pdInSorted = (double*) malloc(sizeof(double) *iLen);
if (pdInSorted == NULL) if (pdInSorted == NULL)
_mem_error("calculateMedian: Failed to allocate memory for pdInSorted"); _mem_error("calculateMedian: Failed to allocate memory for pdInSorted");
@ -1003,10 +1010,9 @@ MMSignal *convoluteSignals(MMSignal *pmmsInA, MMSignal *pmmsInB) {
// Calculate output length // Calculate output length
int iOutLen = pmmsInA->iNumValues + pmmsInB->iNumValues - 1; int iOutLen = pmmsInA->iNumValues + pmmsInB->iNumValues - 1;
double *pdOut = NULL; // FIXME: This should be added in the final version // Create the output signal to write directly to the destination
pdOut = (double*) malloc(sizeof(double) *iOutLen); MMSignal *signal = createSignalWithDefault(iOutLen, 0);
if (pdOut == NULL) double *pdOut = signal->pdValues;
_mem_error("convoluteSignals: Failed to allocate memory for pdOut");
for (int i = 0; i < iOutLen; i++) { for (int i = 0; i < iOutLen; i++) {
pdOut[i] = 0; pdOut[i] = 0;
@ -1018,7 +1024,7 @@ MMSignal *convoluteSignals(MMSignal *pmmsInA, MMSignal *pmmsInB) {
} }
} }
MMSignal *signal = createSignalFromDoubleArray(iOutLen, pdOut); // Initialize the features of the signal
initMMSignalFeatures(signal); initMMSignalFeatures(signal);
return signal; return signal;
@ -1032,23 +1038,20 @@ MMSignal *getPascalLine(int iLinenum) {
_error("getPascalLine: iLinenum must be greater than 0"); _error("getPascalLine: iLinenum must be greater than 0");
// Calculate a kernel signal using pascales triangle without using the factorial // Calculate a kernel signal using pascales triangle without using the factorial
double *pdValues = NULL; MMSignal *signal = createSignalWithDefault(iLinenum, 1);
pdValues = (double*) malloc(sizeof(double) * iLinenum); double *pdValues = signal->pdValues;
if (pdValues == NULL) if (pdValues == NULL)
_mem_error("getPascalLine: Failed to allocate memory for pdValues"); _mem_error("getPascalLine: Failed to allocate memory for pdValues");
for (int i = 0; i < iLinenum; i++) { for (int i = 0; i < iLinenum; i++) {
pdValues[i] = 1;
for (int j = i-1; j > 0; j--) { for (int j = i-1; j > 0; j--) {
pdValues[j] = pdValues[j] + pdValues[j-1]; pdValues[j] += pdValues[j-1];
} }
} }
MMSignal *signal = createSignalFromDoubleArray(iLinenum, pdValues);
initMMSignalFeatures(signal); initMMSignalFeatures(signal);
free(pdValues);
return signal; return signal;
} }

View File

@ -1,4 +0,0 @@
[ WARN ] 'Test!'
[ WARN ] 'Test!'
[ WARN ] 'Test!'
[ WARN ] 'Test!'

View File

@ -1,4 +0,0 @@
6.000000
7.000000
8.000000
9.000000