Skip to content

Commit

Permalink
A lot memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
Nedja995 committed Dec 10, 2015
1 parent d7c0f1f commit ba080b9
Show file tree
Hide file tree
Showing 10 changed files with 85 additions and 15 deletions.
50 changes: 45 additions & 5 deletions examples/ExamplesManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ enum NPCVTests
CLASSIFYOCR
};


bool Time_Example()
{
NConsolePrint("\nstart Time\n");
Expand All @@ -46,7 +47,7 @@ bool Time_Example()
NConsolePrint("Time struct getting error !");
return false;
}

NConsolePrint("\nTime successfuly used: %02d_%02d_%02d_%02d-%02d-%02d\n", nt->year, nt->month, nt->day, nt->hour, nt->minute, nt->second);

freeN(nt);
Expand All @@ -61,22 +62,30 @@ bool FileWrite_Example()

if (nputils::file_write(output, fileWriteInput) == false) {
NConsolePrint("\nFile Writing failed !");
freeN(output);
return false;
}
freeN(output);
return true;
}

bool ImageReadWrite_Example()
{
char *input = Examples::getPath_alloc(lenaInput);
char *output = Examples::getPath_alloc(writeOutput);
Image * img = nputils::ImageStream::ReadImage_STB(input);

freeN(input);

if (img == NULL) {
NConsolePrint("\nEdge Image Read Write failed! input image not found!");
return false;
}

nputils::ImageStream::WriteImage_STB(img, output);


free_image(img);
freeN(output);
return true;
}

Expand All @@ -86,6 +95,9 @@ bool ImageGray_Example()
char *output = Examples::getPath_alloc(grayOutput);

Image * img = nputils::ImageStream::ReadImage_STB(input);

freeN(input);

if (img == NULL) {
NConsolePrint("\nGray failed! input image not found!");
return false;
Expand All @@ -94,6 +106,8 @@ bool ImageGray_Example()
npip::gray(img);
nputils::ImageStream::WriteImage_STB(img, output);

free_image(img);
freeN(output);

return true;
}
Expand All @@ -113,6 +127,11 @@ bool Subarea_Example()
img->width / 2, img->height / 2);

nputils::ImageStream::WriteImage_STB(small, output);

freeN(input);
freeN(output);
free_image(img);
free_image(small);

return true;
}
Expand All @@ -132,9 +151,18 @@ bool EdgeDetection_Examples()
npip::contour_draw_custom(img, 1, 20);
nputils::ImageStream::WriteImage_STB(img, output);

freeN(input);
freeN(output);
free_image(img);

return true;
}

void free_l(Link * link)
{
npcf::free_image_classification_data((npcf::ImageClassificationData*) link->data);
}

bool ClassifyOcrSamples_Example()
{
char *input = Examples::getPath_alloc(ocrSamples);
Expand All @@ -159,16 +187,28 @@ bool ClassifyOcrSamples_Example()
subimage = image_get_area(img, x, y, ocrSubimageSize, ocrSubimageSize);

icdTemp = npcf::image_classify(subimage, 12);

free_image(subimage);

if (icdTemp == NULL)
continue;
list_put(classes, icdTemp);

//free_image_classification_data(icdTemp);
si++;
}
}

char *csvContent = npcf::datas_get_format_csv(classes);
//char *csvContent = npcf::datas_get_format_csv(classes);

nputils::file_write(output, csvContent);
//nputils::file_write(output, csvContent);

//free(csvContent);
//list_free_default(classes, free_l);

freeN(input);
freeN(output);
free_image(img);

return true;
}
Expand Down Expand Up @@ -318,7 +358,7 @@ int main(int argc, char** argv)
Subarea_Example();
}
else if (testChoosed == EDGES) {
FileWrite_Example();
EdgeDetection_Examples();
}
else if (testChoosed == CLASSIFYOCR) {
ClassifyOcrSamples_Example();
Expand Down
4 changes: 3 additions & 1 deletion include/base/classification/image_classification.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ namespace npcf {
* @return null if it fails, else an new ImageClassificationData.
*/
ImageClassificationData * image_classification_data_create(int regionsCount);

void free_image_classification_data(ImageClassificationData * data);

/**
* @brief Set region value.
Expand Down Expand Up @@ -76,4 +78,4 @@ namespace npcf {
}


#endif
#endif
5 changes: 5 additions & 0 deletions include/base/core/Image.h
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,11 @@ namespace npcore
*/
char *image_string_alloc(const Image *image);


void free_image(Image *image);



/**
* @brief Image trace.
*
Expand Down
8 changes: 8 additions & 0 deletions src/base/classification/image_classification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ namespace npcf {
memsetN(ret->regionsDatas + i, 0, sizeof(int));
return ret;
}

void free_image_classification_data(ImageClassificationData * data)
{
freeN(data->regionsDatas);
freeN(data);
}

bool image_classify_region_set(ImageClassificationData *icd, int region, int val)
{
Expand Down Expand Up @@ -80,6 +86,8 @@ namespace npcf {
regIt += (int)(y / regionSize);

image_classify_region_add(ret, regIt, (px->b == 0) ? 0 : 1);

freeN(px);
}

}
Expand Down
11 changes: 9 additions & 2 deletions src/base/core/Image.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#include "base/core/Image.h"
#include "base/core/npstring.h"


//#include "stdlib.h"

namespace npcore
{
Expand All @@ -16,10 +16,16 @@ namespace npcore
ret->height = height;
ret->width = width;
ret->type = type;

//freeN(ret->data);
return ret;
}

void free_image(Image *image)
{
freeN(image->data);
freeN(image);
}

Pixel * image_get_pixel(const Image * image, int x, int y)
{
uchar * pixel = 0;
Expand Down Expand Up @@ -106,6 +112,7 @@ namespace npcore
}
}
ret = image_create(newData, width , height, image->type);
freeN(newData);
}
return ret;
}
Expand Down
2 changes: 1 addition & 1 deletion src/base/core/debug.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ namespace npcore {
traceFile.close();

}
freeN(_np_trace_log_text_list);
list_free_default(_np_trace_log_text_list);

return 1;
}
Expand Down
3 changes: 2 additions & 1 deletion src/base/core/list.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ namespace npcore
LIST_FOREACH(list){
freeN((void *)link->data);
}
freeN(list);
}

void list_free_default(List * list, NListFree_Callback freeCallback)
Expand All @@ -57,4 +58,4 @@ namespace npcore
(*freeCallback)(link);
}
}
}
}
7 changes: 4 additions & 3 deletions src/base/imageproc/ImageProc_Gray.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "imageproc_gray.h"


#include "base/core/memory.h"
using namespace npcore;
namespace npip{

bool gray(Image * image)
Expand All @@ -13,8 +13,9 @@ namespace npip{
R(px) /= 2;
G(px) /= 2;
B(px) /= 2;
freeN(px);
}
}
return false;
}
}
}
8 changes: 6 additions & 2 deletions src/base/utils/ImageStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,18 @@ namespace nputils
NLogError("\nCannot load image from: %s", pat);
return NULL;
}

ret = image_create(data, width, height, type);

//free_image(ret);
freeN(data);

return ret;
}

bool ImageStream::WriteImage_STB(const Image * image, const char * filepath)
{
int res = stbi_write_png(filepath, image->width, image->height, image->type, image->data, 0);
//int res = stbi_write_png(filepath, image->width, image->height, image->type, image->data, 0);

return true;
}
Expand Down
2 changes: 2 additions & 0 deletions src/base/utils/file_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ namespace nputils

fwrite(text, sizeof(char), textLen, fp);

fclose(fp);

return true;
}

Expand Down

0 comments on commit ba080b9

Please sign in to comment.