🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #10479

CopyMemory API problem

#10479
From: Martin C
Date: Sat, 10 Apr 2004 14:58
101 lines
3722 bytes
Hi All,

I am using the following code to free memory on my machine, although I
always get access violation once the CopyMemory function is reached, can
someone help me please and tell me what exactly I am doing wrong.
Thanks in advance

BR.

#define DIV 1024
#define WIDTH 7
int Granularity;
MEMORYSTATUS stat;

void __fastcall TForm1::Button2Click(TObject *Sender)
{
  int UserEntry, PagesToFree, BytesToFree, WriteAddress, ReadAddress,
ProgressVal;
  AnsiString strTemp;
  LPVOID BaseAddr;
  const int nkb = 1024;

  UserEntry = (StrToInt(Edit1->Text) * nkb) * nkb;
  PagesToFree = UserEntry / Granularity;
  BytesToFree = PagesToFree * Granularity;


  if(BytesToFree <= 0)
  { lblCantFree->Caption = "Not a valid positive number";
    lblCantFree->Visible = true;
    ShowMessage("Error");
    return;
  }

  if(BytesToFree <= stat.dwAvailPhys)
  { lblCantFree->Caption = "That amount is already free";
    lblCantFree->Visible = true;
    ShowMessage("Error");
    return;
  }
  ProgressBar1->Min = 0;
  ProgressBar1->Max = PagesToFree;
  ProgressBar1->Position = 0;

  strTemp = Granularity;
  BaseAddr = VirtualAlloc(NULL, BytesToFree, MEM_RESERVE, PAGE_NOCACHE |
PAGE_READWRITE);

  if(BaseAddr == 0)
  { lblCantFree->Caption = "MEM_RESERVE failed";
    lblCantFree->Visible = true;
    ShowMessage("Error");
    return;
  }

  BaseAddr = VirtualAlloc(BaseAddr, BytesToFree, MEM_COMMIT, PAGE_NOCACHE |
PAGE_READWRITE);
  if(BaseAddr == 0)
  { VirtualFree(BaseAddr, 0, MEM_RELEASE);
    lblCantFree->Caption = "MEM_COMMIT failed";
    lblCantFree->Visible = true;
    ShowMessage("Error");
    return;
  }

  WriteAddress = (int)BaseAddr;
  ReadAddress = StrToInt(strTemp);
  ShowMessage(Granularity);

  while(WriteAddress < ((int)BaseAddr + BytesToFree))
  {
    CopyMemory(&WriteAddress, &ReadAddress, Granularity); // Error shows
here
    WriteAddress = WriteAddress + Granularity;
    ProgressVal = ProgressVal + 1;
    ProgressBar1->Position = ProgressVal;
  }
  VirtualFree(BaseAddr, BytesToFree, MEM_DECOMMIT);
  VirtualFree(BaseAddr, 0, MEM_RELEASE);
  ProgressBar1->Visible = false;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  GlobalMemoryStatus (&stat);
  Label7->Caption=stat.dwMemoryLoad;
  Label8->Caption=stat.dwTotalPhys;
  Label9->Caption=stat.dwAvailPhys;
  Label10->Caption=stat.dwTotalPageFile;
  Label11->Caption=stat.dwAvailPageFile;
  Label12->Caption=stat.dwTotalVirtual;
  Label14->Caption=stat.dwAvailVirtual;

  SYSTEM_INFO SysInfo;
  GetSystemInfo(&SysInfo);
  Granularity = SysInfo.dwAllocationGranularity;
  Label16->Caption = IntToStr(Granularity);
}
//--------------------------------------------------------------------------
-
Technical Details
Message-ID: <40786e5f@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,aeea745eda602570,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2004-04-10 15:09:46 PST
From: "Martin C" <martincplus@hotmail.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: CopyMemory API problem
Date: Sat, 10 Apr 2004 16:58:17 +0200
Lines: 103
Organization: V Safe
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1158
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1165
NNTP-Posting-Host: 81.10.4.238
Message-ID: <40786e5f@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1081634399 81.10.4.238 (10 Apr 2004 14:59:59 -0800)
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!news.glorb.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail