Skip to content

Commit

Permalink
Generator: Fixed memory leak due to missing free() after malloc() in …
Browse files Browse the repository at this point in the history
…get_configured_program()

We want to uniformly use C++ functionality, hence replaced malloc() with new and added the missing delete.
  • Loading branch information
karlrupp committed Jan 7, 2014
1 parent e0b3ba2 commit d9e55d4
Showing 1 changed file with 3 additions and 1 deletion.
4 changes: 3 additions & 1 deletion viennacl/generator/generate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,7 @@ namespace viennacl{
* @param force_recompilation if true, the program will be recompiled
*/
inline viennacl::ocl::program & get_configured_program(viennacl::generator::code_generator const & generator, std::list<viennacl::ocl::kernel*> & kernels, bool force_recompilation = false){
char* program_name = (char*)malloc(256*sizeof(char));
char* program_name = new char[256];
generator.make_program_name(program_name);
if(force_recompilation)
viennacl::ocl::current_context().delete_program(program_name);
Expand All @@ -363,6 +363,8 @@ namespace viennacl{
}
viennacl::ocl::program & p = viennacl::ocl::current_context().get_program(program_name);
generator.configure_program(p, kernels);
delete[] program_name;

return p;
}

Expand Down

5 comments on commit d9e55d4

@ddemidov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you use std::vector<char> program_name(256) instead of char*, and forget about delete[] altogether?

@ptillet
Copy link
Collaborator

@ptillet ptillet commented on d9e55d4 Jan 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is an old enough piece of code I wrote at the time I was not aware that std::vector had little if no overhead. This is a very critical portion of the generator, since this is to be executed everytime an operation is launched, not just upon compilation... I probably got slightly paranoid when I wrote this piece of code (hence my initial malloc...) :)

@ddemidov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In that case char program_name[256] should work faster than malloc() :)

@ptillet
Copy link
Collaborator

@ptillet ptillet commented on d9e55d4 Jan 8, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, obviously! This piece of code is old, I was not sure whether or not 256 bytes would have been enough, when I wrote it! I'm starting to be ashamed now!

@ddemidov
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the joy of picking other's code! :)

Please sign in to comment.