Recent

Author Topic: [SOLVED]Accessing a class member causes a memory violation (SIGSEGV)  (Read 3713 times)

yanes19

  • New Member
  • *
  • Posts: 36
Hi all ,
I created a unit in my projects that contains a simple class that runs a TProcess objects and
deals with it's output ,
The TProcess object is declared as a class member ,

but whenever I try to xxxx:= Tprocess.create(nil) ; or even assign a nil it raises the mem violation exception

I need to mention that if I move the declaration to global , everything runs fine  :(, as you can see below

Here's my (shortened ) code :

Code: Pascal  [Select][+][-]
  1. unit UnitDbgrIntf;
  2.  
  3. {$mode objfpc}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, process,UTF8Process   ;
  9.  
  10. type
  11.  
  12.   { TGDBDebugger }
  13.  
  14.   TGDBDebugger = class
  15.   private
  16.     //dbgr: TProcess;
  17.     function CheckGDBRunning: Boolean ;
  18.   protected
  19.     //function LaunchGDBProcess(): Boolean ;  //moved to public section as desperate test
  20.   public
  21.     dbgr: TProcessUTF8;
  22.     constructor Create;
  23.  
  24.     function LaunchGDBProcess(): Boolean ;
  25.  
  26.     procedure init();
  27.  
  28.     //destructor Destroy;
  29.     property DbgrProcess: TProcessUTF8 read dbgr;
  30.     property IsRunning: Boolean read CheckGDBRunning ;
  31.   end;
  32.  
  33. //var           dbgr :TProcess;   //When declared here it works fine !!
  34.  
  35. implementation
  36.  
  37.    constructor TGDBDebugger.Create();
  38.    var run :boolean;   i:integer;
  39.    begin
  40.      dbgr := nil;
  41.      i:=72;
  42.      run:=LaunchGDBProcess();
  43.    end;
  44.    
  45.   procedure TGDBDebugger.init();
  46.   var run :boolean; i:integer;
  47.   begin
  48.     dbgr := nil;  //testing only tough it shouldnt cause problems
  49.     run:=LaunchGDBProcess();
  50.   end;
  51.  
  52.   function TGDBDebugger.LaunchGDBProcess():Boolean ;
  53.   //var dbgr: TProcess; // when declared here it works too
  54.  
  55.   begin
  56.       dbgr :=TProcessUTF8.Create(nil);  // THIS CAUSES A SIGSEGV !!!
  57.       dbgr.Executable := 'gdb';
  58.       dbgr.Parameters.Add('-q');
  59.       //dbgr.Parameters.Add('-i');
  60.       //dbgr.Parameters.Add('mi2');
  61.       dbgr.Parameters.Add('-nx');
  62.       dbgr.Options := [poUsePipes];
  63.       dbgr.Execute;
  64.  
  65.       result:= dbgr.Running;
  66.   end;
  67.  
  68.   function TGDBDebugger.CheckGDBRunning():Boolean ; //let's return false for now!
  69.   begin
  70.     result:= false;
  71.     ///result:= dbgr.Running;
  72.   end;
  73.  
  74. end.
  75.  
  76.  
  77.  
« Last Edit: September 03, 2021, 08:51:49 pm by yanes19 »

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #1 on: August 29, 2021, 10:20:12 pm »
Add "inherited Create" in the constructor to reserve memory for your class member fields like dbgr

yanes19

  • New Member
  • *
  • Posts: 36
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #2 on: August 29, 2021, 11:21:40 pm »
Thanks for replying , I think I need more details please  ;)
since my class doesnt have a parent how can it inherit methods ?
« Last Edit: August 29, 2021, 11:23:19 pm by yanes19 »

jamie

  • Hero Member
  • *****
  • Posts: 7827
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #3 on: August 29, 2021, 11:28:22 pm »
from what I can see, this can only happen if you call INIT() before the Constructor.Create()

The only true wisdom is knowing you know nothing

Bart

  • Hero Member
  • *****
  • Posts: 5743
    • Bart en Mariska's Webstek
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #4 on: August 29, 2021, 11:28:33 pm »
since my class doesnt have a parent how can it inherit methods ?

Every class inherits from the base class: TObject.
These 2 definitions of a class are basically the same:
Code: Pascal  [Select][+][-]
  1. type
  2.   TA = class
  3.   end;
  4.  
  5.   TB = class(TObject)
  6.   end;

Bart

jamie

  • Hero Member
  • *****
  • Posts: 7827
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #5 on: August 29, 2021, 11:31:13 pm »
Of course I don't see where you are setting an instance to that class either.. if you are calling the class directly then I guess that answers the problem..

 Maybe you want CLASS functions but still that isn't going to work either the way you are doing it.
The only true wisdom is knowing you know nothing

yanes19

  • New Member
  • *
  • Posts: 36
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #6 on: August 29, 2021, 11:39:41 pm »
Thanks to all of you guys
my question in short is : How to get the TProcess (dbgr member here ) reserved so when i call the
Tprocess constructor I dont get a mem violation error

I really appreciate your help  :)

yanes19

  • New Member
  • *
  • Posts: 36
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #7 on: August 29, 2021, 11:44:23 pm »
Of course I don't see where you are setting an instance to that class either.. if you are calling the class directly then I guess that answers the problem..
this class is called from a Form unit as
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   DbgEngine:TGDBDebugger;
  4. .
  5. .
  6. .
  7.  
  8. procedure TForm1.FormCreate(Sender: TObject);
  9.  
  10. begin
  11.  
  12.     DbgEngine.Create();
  13.     //DbgEngine.LaunchGDBProcess();  // this was a test, me trying to solve the problem
  14.                        
  15.  

Bart

  • Hero Member
  • *****
  • Posts: 5743
    • Bart en Mariska's Webstek
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #8 on: August 30, 2021, 12:01:22 am »
this class is called from a Form unit as
Code: Pascal  [Select][+][-]
  1. var
  2.   Form1: TForm1;
  3.   DbgEngine:TGDBDebugger;
  4. .
  5. .
  6. .
  7.  
  8. procedure TForm1.FormCreate(Sender: TObject);
  9.  
  10. begin
  11.  
  12.     DbgEngine.Create();
  13.     //DbgEngine.LaunchGDBProcess();  // this was a test, me trying to solve the problem
  14.                        
  15.  

And there you go wrong, use:
Code: Pascal  [Select][+][-]
  1.   ...
  2.   DbgEngine := TGDBDebugger.Create;

That will create an instance of TGDBDebugger and assign it to the DbgEngine variable.

Bart

yanes19

  • New Member
  • *
  • Posts: 36
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #9 on: August 30, 2021, 12:13:41 am »
thanks Bart ,
Yes that's totally correct ,
the main problem is initialising the dbgr variable since it's doesnt seems to be  created ,
Code: Pascal  [Select][+][-]
  1. dbgr :=TProcessUTF8.Create(nil);  // THIS CAUSES A SIGSEGV (memory violation)!!!
  2.  

PascalDragon

  • Hero Member
  • *****
  • Posts: 6403
  • Compiler Developer
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #10 on: August 30, 2021, 11:03:10 am »
Add "inherited Create" in the constructor to reserve memory for your class member fields like dbgr

That is not correct. The memory allocation is done before the code of the constructor is executed and thus also before any inherited Create is executed. It depends on how the constructor is called: the outermost constructor is called in a way that NewInstance will be called, while nested calls to Create will be just like normal method calls.

thanks Bart ,
Yes that's totally correct ,
the main problem is initialising the dbgr variable since it's doesnt seems to be  created ,
Code: Pascal  [Select][+][-]
  1. dbgr :=TProcessUTF8.Create(nil);  // THIS CAUSES A SIGSEGV (memory violation)!!!
  2.  

You misunderstood Bart: your error is that you use DbgEngine.Create() which is wrong, cause in that case there will be no memory allocated for DbgEngine thus your initialization of dbgr fails. You must use DbgEngine := TGDBDebugger.Create and then the initialization of dbgr will work as well.

yanes19

  • New Member
  • *
  • Posts: 36
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #11 on: August 30, 2021, 02:31:44 pm »
I attached the a simple project that reproduces the problem ,
Now I'm sure there something silly happening ,I appreciate it if someone takes a look at it

Best regards,
 Yanes

PascalDragon

  • Hero Member
  • *****
  • Posts: 6403
  • Compiler Developer
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #12 on: August 30, 2021, 02:34:50 pm »
I attached the a simple project that reproduces the problem ,
Now I'm sure there something silly happening ,I appreciate it if someone takes a look at it

Did you even read what we wrote? Your problem is in Unit1.TForm1.btn_DebugRunClick: you need to use DbgEngine := TGDBDebugger.Create and not DbgEngine.Create!

yanes19

  • New Member
  • *
  • Posts: 36
Re: Accessing a class member causes a memory violation (SIGSEGV)
« Reply #13 on: August 30, 2021, 02:48:39 pm »
Thanks ,
My bad , I knew I'm doing something silly here   :-[,
I missed your last post as I didnt refresh the page ,

sorry , feel so dumb now  %) .
Thank you guys ,
I really appreciate your help

Best regards ,

 

TinyPortal © 2005-2018