delphi - Automatically free memory when leaving stack -


i program helps me free memory automatically pointer dynamically allocated address space leaves stack. example be:

procedure fillmemory (var mypointer); begin   // copymemory, move, etc... data end;  procedure myprocedure; var   mypointer : pointer; begin   mypointer := virtualalloc (nil, 1024, mem_commit or mem_reserve, page_execute_readwrite);   fillmemory (mypointer);   virtualfree(mypointer, 0, mem_release); // avoid this... end; 

i use strings avoid them (which i'm not sure if strings in stack freed anyway...) ideas?

to expand on comments , discussion arioch,

if want raw memory block, use dynamic array of byte. compiler generate code release memory @ end of method:

type   tbytes = array of byte; // omit newer delphi versions  procedure fillmemory(var bytes: tbytes); begin   { passing in here increase reference count 2 }   // copymemory, move, etc... data end; // drop 1  procedure myprocedure; var   buffer : tbytes; begin   setlength(buffer, 1024);  // buffer has reference count of 1   fillmemory (buffer); end;   // reference count drop 0, , delphi free memory here 

hope makes sense. midnight here, i'm not feeling awake...


Comments