Recent

Author Topic: Class - when is it destroyed?  (Read 26881 times)

janvb

  • Jr. Member
  • **
  • Posts: 76
Class - when is it destroyed?
« on: August 02, 2014, 11:53:10 pm »
hi, I am trying to understand how classes are destroyed, but I can not find any exact text. as I call a class constructor it is created on the heap, but then is this destroyed?

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Class - when is it destroyed?
« Reply #1 on: August 02, 2014, 11:57:41 pm »
No, there is only one way to destroy an object and that is to call the free method. If not called then object will remain in memory until the application exits (in windows at least) and that is called a memory leak.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Class - when is it destroyed?
« Reply #2 on: August 03, 2014, 12:07:58 am »
However... Some objects have an Owner parameter which you can pass on during create. When doing so it becomes the task of that owner object to do the freeing of that newly created object.

Like for this (calling it with nil) you'll need to free it manually:
Code: [Select]
ed := TEdit.Create(nil);
...
ed.free;

But if you call it like this
Code: [Select]
ed := TEdit.Create(Form1);
...
you don't need to call ed.free because Form1 will destroy the object when it gets destroyed itself (the ed.free is called by Form1 internally on destruction).

janvb

  • Jr. Member
  • **
  • Posts: 76
Re: Class - when is it destroyed?
« Reply #3 on: August 03, 2014, 12:19:45 am »
Thanks - I assume you mean class, because Object create itself on the stack and will be destroyed as it goes out of scope?

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Class - when is it destroyed?
« Reply #4 on: August 03, 2014, 12:31:57 am »
Yeah... I meant Class.

There is some confusion here at what you call an Object or Class.
(just reading this and in that case i do mean class)

I always considered an Object to be an instance of a Class like in Delphi.
« Last Edit: August 03, 2014, 12:42:18 am by rvk »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Class - when is it destroyed?
« Reply #5 on: August 03, 2014, 12:42:50 am »
no a class is the blueprint from where objects are created so a TForm1 is a class Form2 := TForm1.Create; form2 holds a reference to an object in memory not a class.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Class - when is it destroyed?
« Reply #6 on: August 03, 2014, 12:50:49 am »
no a class is the blueprint from where objects are created so a TForm1 is a class Form2 := TForm1.Create; form2 holds a reference to an object in memory not a class.
I think janvb means Object in the sense of
Code: [Select]
Type
  Average = Object
    NumVal: Integer;
    Values: Array [1..200] of Real;
    Function Mean: Real; { calculates the average value of the array }
  End;
as on this page.

But this construction of type object is never (or seldom) used in pascal anymore.

Nowadays we call Object as an instance of a class (like here).

Yeah... the terminology threw me off too  :)

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Class - when is it destroyed?
« Reply #7 on: August 03, 2014, 12:55:34 am »
I think janvb means Object in the sense of
Code: [Select]
Type
  Average = Object
    NumVal: Integer;
    Values: Array [1..200] of Real;
    Function Mean: Real; { calculates the average value of the array }
  End;
as on this page.

But this construction of type object is never (or seldom) used in pascal anymore.

Nowadays we call Object as an instance of a class (like here).

Yeah... the terminology threw me off too  :)

Those are called old school objects they where introduced and used on turbo pascal 5 (I think) up to borland pascal 7. The terminology does not change though. Objects are instantiated classes while classes are the blueprint to create objects this means that while you can have multiple objects in memory there is only one class for all those objects in the executable.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

janvb

  • Jr. Member
  • **
  • Posts: 76
Re: Class - when is it destroyed?
« Reply #8 on: August 03, 2014, 01:17:31 am »
Yes I am talking about the difference between Average=Object and Average=Class + what is dynamic memory allocation and what is not + when a destructor is called.

Reading the language spec:

"The difference between objects and classes is mainly that an object is allocated on the stack, as an ordinary record would be, and that classes are always allocated on the heap."

I need to test a few things!

This is a clumsy way to differ wherever you declar on stack or in dynamic memory - dynamic memory comes at a performance price, so more stack usage is one way to speed up code.

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Class - when is it destroyed?
« Reply #9 on: August 03, 2014, 01:30:17 am »
Here is a good source:
http://www.tutorialspoint.com/pascal/pascal_object_oriented.htm

It says for what i and taazz called objects:
Quote
Instantiation of a class
Instantiation means creating a variable of that class type. Since a class is just a pointer, when a variable of a class type is declared, there is memory allocated only for the pointer, not for the entire object. Only when it is instantiated using one of its constructors, memory is allocated for the object. Instances of a class are also called 'objects', but do not confuse them with Object Pascal Objects. In this tutorial, we will write 'Object' for Pascal Objects and 'object' for the conceptual object or class instance.

So in your case what you call Objects are "Pascal Objects" and we mean "Instantiation of a class".

When in the future you're discussing Objects keep in mind the other parties will likely also think about an "Instantiation of a class".

As far as i now "Pascal Objects" don't really need a constructor and destructor because you declare them directly in the var-section and they get cleaned up when leaving the procedure (just like other variables/records).

I'm not sure what the real downsides are of using the old school Pascal Objects (but i imagine there are some, like running out of stack-space for one).
« Last Edit: August 03, 2014, 01:35:09 am by rvk »

janvb

  • Jr. Member
  • **
  • Posts: 76
Re: Class - when is it destroyed?
« Reply #10 on: August 03, 2014, 01:57:30 am »
I'm not sure what the real downsides are of using the old school Pascal Objects (but i imagine there are some, like running out of stack-space for one).

Jupp.



Leledumbo

  • Hero Member
  • *****
  • Posts: 8799
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Class - when is it destroyed?
« Reply #11 on: August 03, 2014, 07:00:11 am »
As far as i now "Pascal Objects" don't really need a constructor and destructor because you declare them directly in the var-section and they get cleaned up when leaving the procedure (just like other variables/records).

I'm not sure what the real downsides are of using the old school Pascal Objects (but i imagine there are some, like running out of stack-space for one).
If you ever read tutorials using old style object, they will often do it in the C++ style. e.g.: the object type is declared along with a pointer to it, and that pointer is used instead of the type directly. Hence the need for extended New() for calling constructor.

rvk

  • Hero Member
  • *****
  • Posts: 6716
Re: Class - when is it destroyed?
« Reply #12 on: August 03, 2014, 07:59:19 am »
If you ever read tutorials using old style object...
I even worked with them in the late 80's, early 90's  :D
They still didn't strictly need an Init constructor but it was good practice to initialize your variables. And yes... the New-command needed to be adjusted to work with the new Init-call (as did the Dispose-command).

But here we where looking for the advantage over using Classes. When using pointers to Pascal Objects and creating them on the heap they become just like classes so there would be no advantage. And you would still need to call Dispose to get rid of the instance (which is not necessary for the direct variable).

The question is... is there any advantage (or downside) over using a (small) Pascal Object directly as variable, because it will be on the stack. Is the really a noticeable speed difference?

I personally wouldn't use a Pascal Object directly in a variable, even if it was on the stack. I learned early on that stack space is very limited and when doing lots of deep procedure calls you never know how much stack-space you still have left.
« Last Edit: August 03, 2014, 08:02:36 am by rvk »

taazz

  • Hero Member
  • *****
  • Posts: 5368
Re: Class - when is it destroyed?
« Reply #13 on: August 03, 2014, 08:22:22 am »
The question is... is there any advantage (or downside) over using a (small) Pascal Object directly as variable, because it will be on the stack. Is the really a noticeable speed difference?

If we believe the author of aggpas (a vector library) it is substantial enough that he chose it over the classic classes. If you do take in to account that in a library like that you might have thousands of primitives (circles, lines,polylines and other shapes) it becomes even faster as the count goes up.
Good judgement is the result of experience … Experience is the result of bad judgement.

OS : Windows 7 64 bit
Laz: Lazarus 1.4.4 FPC 2.6.4 i386-win32-win32/win64

janvb

  • Jr. Member
  • **
  • Posts: 76
Re: Class - when is it destroyed?
« Reply #14 on: August 03, 2014, 10:34:07 am »
The question is... is there any advantage (or downside) over using a (small) Pascal Object directly as variable, because it will be on the stack. Is the really a noticeable speed difference?

Yes - using the stack you only call the constructor ( and a stack can easily be set to 1Mb etc) and destructor. Using the heap you allocate memory, call the constructor, call the destructor and free memory.

It is the dynamic memory operation of allocating and free'ing that is performance heavy. You don't notice that on normal operations, but do this in the middle of a heavy screen update on thousands of objects and you will see the difference.

The second issue is memory fragmentation over time. In a GUI that is no-care because you restart the application within a month - on a real-time backend system this will kneel down both Windows, Linux and Solaris and probably every known OS after 1-2 years in operation.

That said - even stack usage require the constructor for variable initialization, so a better solution if you need to care about this is to create the class'es outside real-time loops and accept a static trade-off.

 

TinyPortal © 2005-2018