• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 351
  • Last Modified:

Whats wrong with this simple class?!

Hi All,

I was fairly good with Delphi programming a few years back, but have maily been doing C++ since then.
Anyway, to cut a long story short, I needed to make a class that will manage a linked list of processes.

My code is as below:


  //  DATA TYPES //-------------------------------------------------------------
  type PS_PRIO = (PS_FIRST=0, PS_HIGH, PS_NORM, PS_LOW, PS_LAST);
  type PTRProcess = ^process_;
    process_ = record
       next : PTRProcess;
       prev : PTRProcess;
       pid : Integer;
       p_name : String[30];
       priority : PS_PRIO;
       life : Integer;
       status : Integer;
       last_queued : Integer;
       io_bursts : Integer;
       end;


// CLASSES //-----------------------------------------------------------------

// ########## CProcessManager  ##########

  type CProcessManager = class
    private
      firstssh : boolean;
      m_first : PTRProcess;
      m_last : PTRProcess;
   // protected
    public
      constructor Create();
      destructor Destroy();
      function NewProcess() : integer;
      function GetFirstProcess() : integer;//PTRProcess
  end;

//------------------------------------------------------------------------------

  //  FUNCTION DEFS //-----------------------------------------------------
  function ps_to_str(value : PS_PRIO) : string;

implementation


// ---- CProcessManager function definitions ----

constructor CProcessManager.Create();
begin

end;

destructor CProcessManager.Destroy();
begin

end;

function CProcessManager.GetFirstProcess() : integer;
begin
  //Result := firstssh;
  exit;
end;

function CProcessManager.NewProcess() : integer;
var p_process : PTRProcess;
begin
// Make first process
  New(p_process);
  firstssh := TRUE;   <----- error happens here. Note it has nothing to do with the dynamic allocation above.
  p_process^.pid := 101;
  ShowMessage(inttostr(p_process^.pid));
end;


end.

So ignoring the fact that the code does not do anything usefull... Whenever I run the code, I get an exception
when I try and set the value of firstssh.  It doesnt matter what type or name I choose for the field variable in the
class, I always get an error:

"Access violation at address 00458514... Write of address 00000004."

Im sure Ive just made some silly mistake.. this would have worked fine in C++.

Some fast help is well appreciated.
0
shamstar
Asked:
shamstar
  • 3
  • 2
1 Solution
 
Russell LibbySoftware Engineer, Advisory Commented:
Try the following

constructor CProcessManager.Create();
begin
 inherited Create;
end;

destructor CProcessManager.Destroy();
begin
 inherited Destroy;
end;


Regards,
Russell
0
 
shamstarAuthor Commented:
Nope that didn't make it work., but thanks for your post.
Does the above code work for anyone else??
0
 
Russell LibbySoftware Engineer, Advisory Commented:
It worked fine for me (even without the inherited usage)

unit Unit2;

interface

uses
  Windows, SysUtils, Messages, Forms, Dialogs;

//  DATA TYPES //-------------------------------------------------------------
type PS_PRIO = (PS_FIRST, PS_HIGH, PS_NORM, PS_LOW, PS_LAST);
  type PTRProcess = ^process_;
    process_ = record
       next : PTRProcess;
       prev : PTRProcess;
       pid : Integer;
       p_name : String[30];
       priority : PS_PRIO;
       life : Integer;
       status : Integer;
       last_queued : Integer;
       io_bursts : Integer;
       end;


// CLASSES //-----------------------------------------------------------------

// ########## CProcessManager  ##########

  type CProcessManager = class
    private
      firstssh : boolean;
      m_first : PTRProcess;
      m_last : PTRProcess;
   // protected
    public
      constructor Create();
      destructor Destroy();
      function NewProcess() : integer;
      function GetFirstProcess() : integer;//PTRProcess
  end;

//------------------------------------------------------------------------------

  //  FUNCTION DEFS //-----------------------------------------------------
//   function ps_to_str(value : PS_PRIO) : string;

implementation


// ---- CProcessManager function definitions ----

constructor CProcessManager.Create();
begin

end;

destructor CProcessManager.Destroy();
begin

end;

function CProcessManager.GetFirstProcess() : integer;
begin
  //Result := firstssh;
  exit;
end;

function CProcessManager.NewProcess() : integer;
var p_process : PTRProcess;
begin
// Make first process
  New(p_process);
  firstssh := TRUE;
  p_process^.pid := 101;
  ShowMessage(inttostr(p_process^.pid));
end;


end.

------------------

var  cp:      CProcessManager;
begin

  cp:=CProcessManager.Create;
  if cp.NewProcess = 0 then
     beep;

end;


Regards,
Russell

0
 
shamstarAuthor Commented:
Thanks for trying.. you also showed me the stupid mistake I made :-)
I wasn't creating the object properly.. I had something like

var  cp:      CProcessManager;
begin

  if cp.NewProcess = 0 then
     beep;

end;

So missed the cp:=CProcessManager.Create;

Knew it had to br something simple - thanks!
0
 
Russell LibbySoftware Engineer, Advisory Commented:
No problem ;-)

Happy coding,
Russell

0

Featured Post

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now