+ Reply to Thread
Results 1 to 5 of 5
Like Tree2Likes
  • 1 Post By LeFF
  • 1 Post By SqUeEzEr

Thread: VirtualAlloc and ReportMemoryLeaksOnShutdown

  1. #1
    Senior Member cracksman's Avatar
    Join Date
    Dec 2006
    Location
    behind your little sister
    Posts
    1,611

    VirtualAlloc and ReportMemoryLeaksOnShutdown

    just a random question, can anyone explain to me why VirtualAlloc is not causing memory leaks? (GetMem & GetMemory do cause them)
    Code:
    program Project1;
    
    {$APPTYPE CONSOLE}
    
    uses
      Windows;
    Var
      Mem : Pointer;
    begin
      ReportMemoryLeaksOnShutdown := True;
      //GetMem(Mem, 256);//<-creates memory leak
      //Mem := GetMemory(256);//<-creates memory leak
      Mem := VirtualAlloc(Nil, 256, MEM_RESERVE, PAGE_READWRITE);//<- no leak reported 
    end.
    Last edited by cracksman; 30-01-2012 at 18:57.
    I Retired. stop asking me questions. you can find me on msn or ic0de.

  2. #2
    Member bigrig's Avatar
    Join Date
    Jan 2012
    Posts
    92
    http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx

    MEM_RESERVE
    0x2000
    Reserves a range of the process's virtual address space without allocating any actual physical storage in memory or in the paging file on disk.

    if add "MEM_COMMIT" flag, it will cause leak

  3. #3
    Senior Member LeFF's Avatar
    Join Date
    Mar 2011
    Posts
    409
    if add "MEM_COMMIT" flag, it will cause leak
    really? then... does anyone can explain to me how the hell a WinAPI function would report a memory leak to a Delphi runtime? and by the way reserving a virtual memory page and not freeing it is actually a leak too...

    just a random question, can anyone explain to me why VirtualAlloc is not causing memory leaks?
    because you don't understand what you are doing... again VirtualAlloc is a WinAPI function that is why...

    ADD: just to make it clear... detecting memory leaks are not operating system feature, it's a part of language features... just like a garbage collection is a part of runtime in D, Go or Python languages for example... GetMemory function is a part of Delphi runtime (just like operator new are part of C++ and D runtime for example), it is not a operating system function... the code of GetMemory function provides additional code for leaks detection and calls operating system function to allocate memory, if we are talking about windows I bet it would be HeapAlloc (kernel32.dll) or malloc (msvcrt.dll), if we are talking about linux I bet it would be malloc (libc.so)...
    Last edited by LeFF; 31-01-2012 at 08:29.
    cracksman likes this.

  4. #4
    Senior Member cracksman's Avatar
    Join Date
    Dec 2006
    Location
    behind your little sister
    Posts
    1,611
    @bigrig; no.
    @LeFF; thanks that makes perfect sense, but what I did is causing a leak, so I think it's weird that it's not being reported. :/

    [edit]
    just in case anyone is still confused. there is a leak, but apparently ReportMemoryLeaksOnShutdown only reports leaks reported by the Delphi wrapper, so it doesn't report on winapi :/
    Last edited by cracksman; 31-01-2012 at 16:39.
    I Retired. stop asking me questions. you can find me on msn or ic0de.

  5. #5
    Senior Member
    Join Date
    Jun 2008
    Location
    0x40000
    Posts
    1,523
    @LeFF; Cracksman could have thought that embarcadero implented a feature that monitors all created memory and they were keeping track of all allocated memory. Unfortunatly, they do not do this and you will have to write a wrapper yourself if you want to deal with this sort of memory leaks.
    cracksman likes this.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 ©2011, Crawlability, Inc.