Rosegarden

Rosegarden-logo-dark-300x129

Resource leak

V668 There is no sense in testing the ‘file’ pointer against null, as the memory was allocated using the ‘new’ operator. The exception will be generated in the case of memory allocation error. SF2PatchExtractor.cpp 94

SF2PatchExtractor::Device
SF2PatchExtractor::read(string fileName)
{
  Device device;

  ifstream *file = new ifstream(fileName.c_str(), ios::in |....);
  if (!file)
    throw FileNotFoundException();
  ....
}

There is an interesting code fragment for file handling. It seems that a developer inspired by such programming languages as C# and Java. Otherwise, it is not clear why not to create an instance of ifstream type just as a variable on the stack. Dynamic memory allocation is clearly redundant and, in addition, caused an error.

Here is a list of issues of this code fragment:

  • Code is excessively difficult;
  • A check of the pointer here makes no sense (new operator will throw an exception if it cannot allocate memory for an object);
  • The situation with the file absence is ignored;
  • Memory leak, because a pointer is not deallocated anywhere.

Please click here to see more bugs from this project.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s