Hi, I've run Clang Static Analyzer checker alpha.cplusplus.NewDeleteLeaks over LLVM codebase to detect false-positives and at the same time eliminate memory leaks. The majority of leaks were detected in lib/Target/* and lib/MC/*. In all cases the similar trick was detected as a leak (example from lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.cpp) : static MCStreamer *createMCStreamer(const Target &T, StringRef TT, MCContext &Context, MCAsmBackend &MAB, raw_ostream &OS, MCCodeEmitter *Emitter, const MCSubtargetInfo &STI, bool RelaxAll, bool NoExecStack) { MCStreamer *S createELFStreamer(Context, MAB, OS, Emitter, RelaxAll, NoExecStack); new SparcTargetELFStreamer(*S); 1 Memory is allocated ? return S; 2 ? Potential memory leak } Have not got why is this SparcTargetELFStreamer created dynamically and not assigned. Can anybody please explain is this a trick or a trouble? The similar pattern was detected in lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.cpp lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.cpp lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.cpp lib/MC/MCELFStreamer.cpp lib/MC/WinCOFFStreamer.cpp lib/MC/MCMachOStreamer.cpp -- Anton -------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20140311/61c395d6/attachment.html>
On Mon, Mar 10, 2014 at 4:24 PM, Anton Yartsev <anton.yartsev at gmail.com> wrote:> Hi, > > I've run Clang Static Analyzer checker alpha.cplusplus.NewDeleteLeaks over > LLVM codebase to detect false-positives and at the same time eliminate > memory leaks. The majority of leaks were detected in lib/Target/* and > lib/MC/*. In all cases the similar trick was detected as a leak (example > from lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.cpp) : > > static MCStreamer *createMCStreamer(const Target &T, StringRef TT, > MCContext &Context, MCAsmBackend &MAB, > raw_ostream &OS, MCCodeEmitter *Emitter, > const MCSubtargetInfo &STI, bool > RelaxAll, > bool NoExecStack) { > MCStreamer *S > createELFStreamer(Context, MAB, OS, Emitter, RelaxAll, NoExecStack); > new SparcTargetELFStreamer(*S); > > 1 Memory is allocated → > > return S; > > 2 ← Potential memory leak > } > > Have not got why is this SparcTargetELFStreamer created dynamically and not > assigned. > Can anybody please explain is this a trick or a trouble?It's a trick - if you look at the MCTargetStreamer ctor (which SparcTargetELFStreamer derives from you'll see this: MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) { S.setTargetStreamer(this); } So it didn't actually leak... the MCStreamer ('S' in the function you showed above) took ownership of the object.> > The similar pattern was detected in > lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp > lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.cpp > lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp > lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp > lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.cpp > lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp > lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.cpp > lib/MC/MCELFStreamer.cpp > lib/MC/WinCOFFStreamer.cpp > lib/MC/MCMachOStreamer.cpp > > -- > Anton > > > _______________________________________________ > LLVM Developers mailing list > LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu > http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >
On 11.03.2014 3:34, David Blaikie wrote:> On Mon, Mar 10, 2014 at 4:24 PM, Anton Yartsev <anton.yartsev at gmail.com> wrote: >> Hi, >> >> I've run Clang Static Analyzer checker alpha.cplusplus.NewDeleteLeaks over >> LLVM codebase to detect false-positives and at the same time eliminate >> memory leaks. The majority of leaks were detected in lib/Target/* and >> lib/MC/*. In all cases the similar trick was detected as a leak (example >> from lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.cpp) : >> >> static MCStreamer *createMCStreamer(const Target &T, StringRef TT, >> MCContext &Context, MCAsmBackend &MAB, >> raw_ostream &OS, MCCodeEmitter *Emitter, >> const MCSubtargetInfo &STI, bool >> RelaxAll, >> bool NoExecStack) { >> MCStreamer *S >> createELFStreamer(Context, MAB, OS, Emitter, RelaxAll, NoExecStack); >> new SparcTargetELFStreamer(*S); >> >> 1 Memory is allocated → >> >> return S; >> >> 2 ← Potential memory leak >> } >> >> Have not got why is this SparcTargetELFStreamer created dynamically and not >> assigned. >> Can anybody please explain is this a trick or a trouble? > It's a trick - if you look at the MCTargetStreamer ctor (which > SparcTargetELFStreamer derives from you'll see this: > > MCTargetStreamer::MCTargetStreamer(MCStreamer &S) : Streamer(S) { > S.setTargetStreamer(this); > } > > So it didn't actually leak... the MCStreamer ('S' in the function you > showed above) took ownership of the object.Ah, got it, thank you! Missed passing 'this'.> >> The similar pattern was detected in >> lib/Target/ARM/MCTargetDesc/ARMELFStreamer.cpp >> lib/Target/ARM/MCTargetDesc/ARMMCTargetDesc.cpp >> lib/Target/PowerPC/MCTargetDesc/PPCMCTargetDesc.cpp >> lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp >> lib/Target/XCore/MCTargetDesc/XCoreMCTargetDesc.cpp >> lib/Target/Mips/MCTargetDesc/MipsMCTargetDesc.cpp >> lib/Target/Sparc/MCTargetDesc/SparcMCTargetDesc.cpp >> lib/MC/MCELFStreamer.cpp >> lib/MC/WinCOFFStreamer.cpp >> lib/MC/MCMachOStreamer.cpp >> >> -- >> Anton >> >> >> _______________________________________________ >> LLVM Developers mailing list >> LLVMdev at cs.uiuc.edu http://llvm.cs.uiuc.edu >> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev >>-- Anton