Objects added two times: problem o misunderstanding?

Moderator: Rathinagiri

User avatar
l3whmg
Posts: 694
Joined: Mon Feb 23, 2009 8:46 pm
Location: Italy
Contact:

Objects added two times: problem o misunderstanding?

Post by l3whmg » Wed Jun 08, 2011 9:28 pm

Hi friends.
I'm trying to find solution for some problems and for this reason I create a little fork of HMG4 with copy and paste.
I think I've found a problem or perhaps I can't understand the source code. Can you help me?

Problem: when we add the current object to its parent (to use syntax FormName:Control), I think we add two times the same object with different names.

Example: button.

Code from button.prg

Code: Select all

METHOD New( oParent ) CLASS BUTTON
   Super:New( oParent )  ====>0
   ::oQTObject := QPushButton( ::oParent:oQTObject )
   ::oFont := ::oQTObject:Font()
   ::oParent:AddData( ::cName , Self ) ====>1
Code from control.prg

Code: Select all

METHOD New( oParent ) CLASS CONTROL
   IF !::lGridCellFlag
      ::nObjectCounter++
      ::cName := '_HMG_OBJECT_' + AllTrim( Str( Int( ::nObjectCounter ) ) )   ====>0
====>0 Here, we assign the name _HMG_OBJECT_xxxxxxx
====>1 Here, we add the object named _HMG_OBJECT_xxxxxxx to its parent (ie now we can use FormName:_HMG_OBJECT_xxxxxxx)

Code from basic.prg

Code: Select all

METHOD Name( cValue ) CLASS BASIC
   IF Pcount() == 0
      RETURN ::cName
   ELSEIF Pcount() == 1
      IF ::cName != cValue
         ::cName := cValue
         ::oParent:AddData( ::cName, Self ) ====> 2
====>2 Here, we assign AND CHANGE the name. We add the object named "button1" to its parent (ie now we can use FormName:button1)

source code WITHOUT use of :Name method

Code: Select all

WITH OBJECT oWin := Window():New()
  WITH OBJECT Button():New()
  END WITH
END WITH
we have added only ONE time an object (named _HMG_OBJECT_xxxxxxx) to its parent

source code WITH use of :Name method

Code: Select all

WITH OBJECT oWin := Window():New()
  WITH OBJECT Button():New()
   :Name := "Button1"
  END WITH
END WITH
we have added, the same object, TWO times: one time named _HMG_OBJECT_xxxxxxx and another time named button1

My analysis is correct or is a misunderstanding? If it's correct, IMHO we have the risk of unnecessarily fill the memory.

Best regards and many thanks in advance for any help.
Luigi from Italy
www.L3W.it

User avatar
Rathinagiri
Posts: 5263
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 169 times
Been thanked: 172 times
Contact:

Post by Rathinagiri » Thu Jun 09, 2011 4:37 am

Let me check that Luigi. Thanks for the finding.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
concentra
Posts: 256
Joined: Fri Nov 26, 2010 11:31 am
Location: Piracicaba - Brasil

Post by concentra » Thu Jun 09, 2011 9:41 am

Hi Luigi.
I already noted tat some time ago.
I didn´t played much attention on it because I didn´t know if internally the automatic name was used or if this could be coded that way, in order to be possible to have multiple names to the same object.
I think it´s easy to change the Name method to eliminate the current name before add the new one.
[[]] Mauricio Ventura Faria

User avatar
Rathinagiri
Posts: 5263
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 169 times
Been thanked: 172 times
Contact:

Post by Rathinagiri » Thu Jun 09, 2011 9:57 am

In this regard, I want to stress these points for HMG backward compatibility and also for better programming.

1. Once a window/control is defined the name of the window/control object should be a public variable which could be available to all the functions until the window/control is released.

2. Accidentally there should not be any overwritten windows/controls. ie., before creating a window/control, we must check whether there is already a window/control defined in this name.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
l3whmg
Posts: 694
Joined: Mon Feb 23, 2009 8:46 pm
Location: Italy
Contact:

Post by l3whmg » Thu Jun 09, 2011 12:05 pm

Hi guys.
I want remember that, when I have presented my POV, I solved this problem with a rule: the name always in the New() method! In this scenario, work fine OOP and old style.
In my code I had

Code: Select all

METHOD HMGButton:New( cName, oParent )
...
// set-up name
::cName := IIF( VALTYPE( cName ) == "C", cName, "_HMG_OBJECT_" + hb_nots( ::nObjectCounter++ ) )
....
And we can loose this syntax ":Name := <name>"

For PUBLIC problem we solve inside hmg.ch:

Code: Select all

WITH OBJECT name := HMGButton:New( name....
What do you think?

Best regards
Luigi from Italy
www.L3W.it

User avatar
concentra
Posts: 256
Joined: Fri Nov 26, 2010 11:31 am
Location: Piracicaba - Brasil

Post by concentra » Tue Jun 14, 2011 3:33 pm

Hi.
Luigi, your idea of passing the name in the New() method isn´t bad, I agree.
Anyway, I added a method in basic.prg to remove the old name of a control from the parents object stack whenever it is renamed.
[[]] Mauricio Ventura Faria

User avatar
concentra
Posts: 256
Joined: Fri Nov 26, 2010 11:31 am
Location: Piracicaba - Brasil

Post by concentra » Tue Jun 14, 2011 3:36 pm

Hi Rathinagiri.
rathinagiri wrote:2. Accidentally there should not be any overwritten windows/controls. ie., before creating a window/control, we must check whether there is already a window/control defined in this name.
This can be intentional and useful in some situations, specially to change dynamically the interface at runtime.
[[]] Mauricio Ventura Faria

User avatar
Rathinagiri
Posts: 5263
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 169 times
Been thanked: 172 times
Contact:

Post by Rathinagiri » Tue Jun 14, 2011 3:45 pm

Yes. I do understand about this. At that time, we can release the previous object and redefine the same again. Isn't it?
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

User avatar
concentra
Posts: 256
Joined: Fri Nov 26, 2010 11:31 am
Location: Piracicaba - Brasil

Post by concentra » Tue Jun 14, 2011 4:04 pm

Is there any difference ? When the variable containing an object is assigned another object the fist one is automatically released, or not ?
[[]] Mauricio Ventura Faria

User avatar
Rathinagiri
Posts: 5263
Joined: Tue Jul 29, 2008 6:30 pm
DBs Used: MariaDB, SQLite, SQLCipher and MySQL
Location: Sivakasi, India
Has thanked: 169 times
Been thanked: 172 times
Contact:

Post by Rathinagiri » Tue Jun 14, 2011 5:51 pm

That is what I am also saying. It will automatically be released. But, in HMG 3 it is purposefully avoided and if you want to create a new control/window already defined, first you have to release the same explicitly. Otherwise, a run time error would be shown.
East or West HMG is the Best.
South or North HMG is worth.
...the possibilities are endless.

Post Reply