Author Topic: HUGE MEMORY LEAK pinpointed!  (Read 63568 times)

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
HUGE MEMORY LEAK pinpointed!
« on: June 20, 2007, 03:08:16 pm »
Hey guys, I think I know why some of the plugins (like the TODO list and perhaps even Code Completion) waste too much memory!

Look!

Code
bool cbRead(wxFile& file, wxString& st, wxFontEncoding encoding)
{
    st.Empty();
    if (!file.IsOpened())
        return false;
    int len = file.Length();
    if(!len)
    {
        file.Close();
        return true;
    }

    char* buff = new char[len+1];
    if (!buff)
    {
        file.Close();
        return false;
    }
    file.Read((void*)buff, len);
    file.Close();
    buff[len]='\0';

DetectEncodingAndConvert(buff, st, encoding);
// NOW WHERE'S DELETE BUFF?????????

    return true;
}

Notice that DetectEncodingAndConvert takes for parameter a const char*, so it means it doesn't do anything to buff. However, buff isn't released at the end. Hence, the leak.

I'll add a delete operator and see if nothing breaks.

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: HUGE MEMORY LEAK pinpointed!
« Reply #1 on: June 20, 2007, 03:12:06 pm »
Update: It works!!!  :o

Memory consumption with the TODO plugin remains *steady* at 42MB!

I wonder how will code completion be affected with this... hmmm :)
Holy Cow...  :shock: 42MB was *WITH* code completion!!!! (No global includes tho, with them it's around 100mb). Without it's *JUST* 27.6MB! Woo hoo!!!!!!!

I'll commit ASAP.

(Hmm.......... code completion does leak. Everytime i parse, memory consumption is increased. But that'll be another adventure! :D )
« Last Edit: June 20, 2007, 03:22:28 pm by rickg22 »

Offline David Perfors

  • Developer
  • Lives here!
  • *****
  • Posts: 560
Re: HUGE MEMORY LEAK pinpointed!
« Reply #2 on: June 20, 2007, 03:13:23 pm »
wow, that is a nice catch :) great work rick :D
OS: winXP
Compiler: mingw
IDE: Code::Blocks SVN WX: 2.8.4 Wish list: faster code completion, easier debugging, refactoring

Offline MortenMacFly

  • Administrator
  • Lives here!
  • *****
  • Posts: 9694
Re: HUGE MEMORY LEAK pinpointed!
« Reply #3 on: June 20, 2007, 03:19:56 pm »
Wow... how ugly... must patch my C::B to have some testing... Well done, Rick! :P
Compiler logging: Settings->Compiler & Debugger->tab "Other"->Compiler logging="Full command line"
C::B Manual: https://www.codeblocks.org/docs/main_codeblocks_en.html
C::B FAQ: https://wiki.codeblocks.org/index.php?title=FAQ

Offline jpaterso

  • Multiple posting newcomer
  • *
  • Posts: 57
Re: HUGE MEMORY LEAK pinpointed!
« Reply #4 on: June 20, 2007, 03:24:48 pm »
Nicely picked up.
Recompiling my C::B now!

Offline rickg22

  • Lives here!
  • ****
  • Posts: 2283
Re: HUGE MEMORY LEAK pinpointed!
« Reply #5 on: June 20, 2007, 03:26:21 pm »
Everybody update their SVN's!

Fix posted at revision 4120!

Offline jpaterso

  • Multiple posting newcomer
  • *
  • Posts: 57
Re: HUGE MEMORY LEAK pinpointed!
« Reply #6 on: June 20, 2007, 03:48:53 pm »
Should it be delete [] instead of delete?

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: HUGE MEMORY LEAK pinpointed!
« Reply #7 on: June 20, 2007, 03:54:34 pm »
Should it be delete [] instead of delete?
YES.
« Last Edit: June 20, 2007, 03:56:19 pm by TDragon »
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline Jan van den Borst

  • Multiple posting newcomer
  • *
  • Posts: 99
Re: HUGE MEMORY LEAK pinpointed!
« Reply #8 on: June 20, 2007, 04:13:13 pm »
Typing in delete by hand is almost always bad practice. Use f.e. auto_ptr.

Offline byo

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 837
Re: HUGE MEMORY LEAK pinpointed!
« Reply #9 on: June 20, 2007, 04:18:59 pm »
Should it be delete [] instead of delete?
YES.

Yup, should be called. Fortunately in this particular case it will work with normal delete since there's no destructor called (binary code will probably look the same after switching to delete[]). So there's no need to panic ;).

Ok, now it's time to build the new c::B. Thanks rickg22 for the fix :)

BYO

Offline Grom

  • Almost regular
  • **
  • Posts: 206
Re: HUGE MEMORY LEAK pinpointed!
« Reply #10 on: June 20, 2007, 04:42:03 pm »
Guys you have to use std::string. That will fix lot's of problems... Specially in that stupid example.
gcc+winXP+suse.

Alturin

  • Guest
Re: HUGE MEMORY LEAK pinpointed!
« Reply #11 on: June 20, 2007, 04:49:58 pm »
Why aren't you using wxString everywhere anyway?
Either that or as Grom said, use std::string!

Also, maybe someone with the required knowledge could run C::B through valgrind or similar?
« Last Edit: June 20, 2007, 04:55:47 pm by Alturin »

Offline byo

  • Plugin developer
  • Lives here!
  • ****
  • Posts: 837
Re: HUGE MEMORY LEAK pinpointed!
« Reply #12 on: June 20, 2007, 05:08:32 pm »
Why aren't you using wxString everywhere anyway?
Either that or as Grom said, use std::string!

wxString CAN NOT be used here because it is used to BUILD proper wxString. And std::string can allocate much more memory than needed. And this code was put into "global functions" because it does some tricky / risky thing and is provided to all developers to prevent them from writing their own (risky/tricky) code. Only because such bug was found in critical function doesn't mean that it's common technique in other parts of code.

Quote
Also, maybe someone with the required knowledge could run C::B through valgrind or similar?

That would require some knowledge since wxWidgets would have to compiled with valgrind first. It would be much easier to use wxWidget's built-in memory leak search features.I remember that when using wx with Visual Studio, such things was turned on automatically and gave nice reports at the end of execution.

BYO

Offline TDragon

  • Lives here!
  • ****
  • Posts: 943
    • TDM-GCC
Re: HUGE MEMORY LEAK pinpointed!
« Reply #13 on: June 20, 2007, 05:18:25 pm »
It would be much easier to use wxWidget's built-in memory leak search features.

Just make sure you get rid of:
Quote from: Seven C::B source files
#undef new
https://jmeubank.github.io/tdm-gcc/ - TDM-GCC compiler suite for Windows (GCC 9.2.0 2020-03-08, 32/64-bit, no extra DLLs)

Offline thomas

  • Administrator
  • Lives here!
  • *****
  • Posts: 3979
Re: HUGE MEMORY LEAK pinpointed!
« Reply #14 on: June 20, 2007, 05:22:15 pm »
Quote
Why aren't you using wxString everywhere anyway?
Well, we could use wxString for that, file.Read(wxStringBuffer(buff, len+1), len); does just that... but it only adds a lot of overhead.

Rather than using a function that allocates buffers and reads data from disk, then makes a copy only to discard it a millisecond later, it would be better to use the FileManager for loading, anyway. This would make use of editors that are already loaded into memory, remove two allocations and memory copies, offer better I/O balancing, and be able to read from URLs.

But for the meantime, this is a nice catch, Rick.
"We should forget about small efficiencies, say about 97% of the time: Premature quotation is the root of public humiliation."