NoLeak - include to help you find memory leaks (PB5.20)

Share your advanced PureBasic knowledge/code with the community.
Fred
Administrator
Administrator
Posts: 18162
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: NoLeak - include to help you find memory leaks (1.4)

Post by Fred »

ClearStructure() is only needed if you call InitializeStructure() somewhen (After AllocateMemory() for example). Beside that, all is automatically managed by PB.
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: NoLeak - include to help you find memory leaks (1.3)

Post by luis »

sec wrote: I get macro error:
Thanks. I did left an old param for catchimage() not present anymore. Hope it's ok now.

And yes, noleak catches only one class of macroscopic errors basically... (mismatched allocations/deallocations through allocatememory/freememory). It's a very limited tool.
It can help in some cases, but cannot do what you can do manually, and I feel trying to add more "intelligence" to it would cause more false reports than not.
For GDI leakages look at the GDIView tool mentioned in my first post (it can't tell you were they are in your code but it can help to verify their presence and the kind of objects leaking).
"Have you tried turning it off and on again ?"
A little PureBasic review
yrreti
Enthusiast
Enthusiast
Posts: 546
Joined: Tue Oct 31, 2006 4:34 am

Re: NoLeak - include to help you find memory leaks (1.4)

Post by yrreti »

Thank you much luis for sharing this program, and infratec for your addin part.
I like this NoLeak detector, as it helps me correct mistakes that lead to leak errors in my programs.
But I like infratec's approach for more location detail. But for what it's worth, I also added the line
'text' to the problem causing line number for a quick reference with out having to go to the line to
see what's wrong. It is in the following code. Just add to luis's source as infratec says on page 2.
I posted it here in case anyone else wants to use it too.

Code: Select all

  
  ;{ DebugNoLeak()
  Procedure DebugNoLeak()
    
    Shared NoLeak_FONT(), NoLeak_ALLOC(), NoLeak_IMAGE()
    Protected i,TheSource$,lp.i,TmpFile$,line_pos.i,thedat$
    
    i = 0
    
    If ListSize(NoLeak_FONT())
      Debug ""
      Debug "NoLeak Font:"
      Debug ""
      ForEach NoLeak_FONT()
        i + 1
        Debug "(" + Str(i) + ") " + "File: " + NoLeak_FONT()\Source$
        line_pos=NoLeak_FONT()\iLine
        TheSource$=NoLeak_FONT()\Source$
        TmpFile$ = GetPathPart(TheSource$)+"tmp.txt"
        While CopyFile(TheSource$, TmpFile$)=0:Wend
        ReadFile(1,TmpFile$)
        lp=0
        While Eof(1) = 0
          thedat$=ReadString(1)
          lp=lp+1
          If lp=line_pos
            Debug "(" + Str(i) + ") " + "Line: " + Str(line_pos)+"  "+thedat$
            Break
          EndIf
        Wend
        CloseFile(1)
        DeleteFile(TmpFile$)
        Debug "(" + Str(i) + ") " + "ID: " + Str(NoLeak_FONT()\iFontNum)
        Debug "(" + Str(i) + ") " + "Name: " + NoLeak_FONT()\Filename$
        Debug "(" + Str(i) + ") " + "Size: " + Str(NoLeak_FONT()\iYsize)
        Debug ""
      Next
      Debug "----------------------------"
    EndIf
    
    If ListSize(NoLeak_ALLOC())
      Debug ""
      Debug "NoLeak Alloc:"
      Debug ""
      ForEach NoLeak_ALLOC()
        i + 1
        Debug "(" + Str(i) + ") " + "File: " + NoLeak_ALLOC()\Source$
        line_pos=NoLeak_ALLOC()\iLine
        TheSource$=NoLeak_ALLOC()\Source$
        TmpFile$ = GetPathPart(TheSource$)+"tmp.txt"
        While CopyFile(TheSource$, TmpFile$)=0:Wend
        ReadFile(1,TmpFile$)
        lp=0
        While Eof(1) = 0
          thedat$=ReadString(1)
          lp=lp+1
          If lp=line_pos
            Debug "(" + Str(i) + ") " + "Line: " + Str(line_pos)+"  "+thedat$
            Break
          EndIf
        Wend
        CloseFile(1)
        DeleteFile(TmpFile$)
        Debug "(" + Str(i) + ") " + "Address: " + Str(NoLeak_ALLOC()\iAddress)
        Debug "(" + Str(i) + ") " + "Size: " + Str(NoLeak_ALLOC()\iBytes)
        Debug ""
      Next
      Debug "----------------------------"
    EndIf
    
    If ListSize(NoLeak_IMAGE())
      Debug ""
      Debug "NoLeak Image:"
      Debug ""
      ForEach NoLeak_IMAGE()
        i + 1
        Debug "(" + Str(i) + ") " + "File: " + NoLeak_IMAGE()\Source$
        line_pos=NoLeak_IMAGE()\iLine
        TheSource$=NoLeak_IMAGE()\Source$
        TmpFile$ = GetPathPart(TheSource$)+"tmp.txt"
        While CopyFile(TheSource$, TmpFile$)=0:Wend
        ReadFile(1,TmpFile$)
        lp=0
        While Eof(1) = 0
          thedat$=ReadString(1)
          lp=lp+1
          If lp=line_pos
            Debug "(" + Str(i) + ") " + "Line: " + Str(line_pos)+"  "+thedat$
            Break
          EndIf
        Wend
        CloseFile(1)
        DeleteFile(TmpFile$)
        Debug "(" + Str(i) + ") " + "ID: " + Str(NoLeak_IMAGE()\iImageNum)
        Debug "(" + Str(i) + ") " + "Width: " + Str(NoLeak_IMAGE()\iWidth)
        Debug "(" + Str(i) + ") " + "Height: " + Str(NoLeak_IMAGE()\iHeight)
        Debug "(" + Str(i) + ") " + "Depth: " + Str(NoLeak_IMAGE()\iDepth)
        Debug ""
      Next
      Debug "----------------------------"
    EndIf
    
    If i > 0
      Debug "NoLeak found " + Str(i) + " leaks!"
    Else
      Debug "NoLeak found no leaks!"
    EndIf
    
  EndProcedure
  ;}
  
CompilerElse
  Macro DebugNoLeak()
  EndMacro
yrreti
User avatar
Danilo
Addict
Addict
Posts: 3036
Joined: Sat Apr 26, 2003 8:26 am
Location: Planet Earth

Re: NoLeak - include to help you find memory leaks (PB5.20)

Post by Danilo »

@luis:
If you change the line

Code: Select all

Macro CreateImage (iImageNum, iWidth, iHeight, iDepth = 24, iBackColor = #Black)
to:

Code: Select all

Macro CreateImage (iImageNum, iWidth, iHeight, iDepth = 24, iBackColor = RGB(0,0,0))
it works also on Mac OS X (and probably GNU/Linux).
User avatar
luis
Addict
Addict
Posts: 3893
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: NoLeak - include to help you find memory leaks (PB5.20)

Post by luis »

@Danilo

I changed it, thanks.
"Have you tried turning it off and on again ?"
A little PureBasic review
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: NoLeak - include to help you find memory leaks (PB5.20)

Post by Lunasole »

Thanks, nice memory-related addition to Purifier and really cool stuff, -1 way to be a fool [million remains] :)
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Post Reply