Giter Site home page Giter Site logo

Comments (7)

benhar-dev avatar benhar-dev commented on August 15, 2024 1

Answering your questions about variables first,

VAR_INST.

VAR_INST inside of a method call is the same as putting the variable in a VAR in the Function Block. Method variables are destroyed at the end of a method call, where as Function Block variables are destroyed only when the Function Block is destroyed.

I personally do not use VAR_INST when doing OOP programming, as I feel that VAR in the body of the function block is easier to read and understand.

VAR_STAT

Static variables. As we know, Function blocks (and Classes) instantiate and hold a collection of variable for themselves. As an example, we can have many TON function blocks which all run independent of each other as their variables are separate.

If you declare a variable as VAR_STAT, you make it static. At that point, all function blocks of the same type will share this variable. If one of the instantiated function blocks writes to it, all of the other function blocks of the same type will see this change. Its a common variable among instances. So. VAR is local, only accessible by the function block. VAR_STAT is common across all instances of a function block.

from oop-iec61131-3--curso-youtube.

benhar-dev avatar benhar-dev commented on August 15, 2024 1

Answering your question about __Delete and could we change the variable so it wouldn't live

A pointer is just an address to an object. If I use __NEW on a function block, it will create that object in memory, and will then update your pointer to point to it.

If you call __Delete on a pointer, it will find the object in memory and mark it for deletion. This deletion may not happen immediately, you have simply told the system you no longer want that object.

The object does not live inside the pointer, a point is just a variable which holds the address of an object, which you can then use ^ to make the pointer appear to be the object it points to. (called dereferencing)

As an example, if I were to do this...

myPointer := __NEW(Heater); // the heater object would be made, and the address of the heater object would go in myPointer
myPointer := 0; // I have just reset the pointer.  The heater object **still exists** in memory.  I've just overwritten my only way to access it

So, the pointer is not the object, only a special variable which holds the address of an object and can also pretend to be the object by using the ^ to dereference it.

The order of events should be..

  1. Create an object using __NEW
  2. Use an object
  3. Mark an an object for deletion once you are finished using __DELETE
  4. (at some point the system will delete the object, most of the time it happens between PLC cycles)

The system will delete objects marked for deletion and we do not fully know (or care) when this will happen as we are already finished with it. This is a system level thing which is done for us.

In your code you are doing the following

  1. Make the circle
  2. Delete the circle
  3. Use the circle <- Here you are using a circle which has already been marked for deletion. Here be danger.
  4. System removes the circle

You can see the bug if you set a breakpoint in your code and single step through it. When single stepping through the code, you will see that they system has already removed your object before you have a chance to use it.

Instead you should remove __DELETE from your clone method. Just return the new object as ITF_Shape as you are currently doing.

You must then implement a way to delete the object. My suggestion is to make ITF_Shape have a .Dispose() method. This way, after you finish drawing the circle you can call .Dispose(). I.e. you mark it for deletion only after you have finished using it.

The circle's dispose method should contain

METHOD Dispose

__DELETE(THIS);

from oop-iec61131-3--curso-youtube.

benhar-dev avatar benhar-dev commented on August 15, 2024 1

Thank you for considering me for this task. However, I’m currently not in a position to directly contribute to the codebase.

from oop-iec61131-3--curso-youtube.

runtimevic avatar runtimevic commented on August 15, 2024

Hi @benhar-dev ,
It's great that you look at it and can contribute, you are welcome...
Design_Pattern_Creational_Prototype_FB_Circle_Method_Clone

  • __DELETE is at the end of all instructions...
  • Could we change the variable like this so it wouldn't live?
    VAR_STAT
VAR
   pClone : POINTER TO Circle;
END_VAR

from oop-iec61131-3--curso-youtube.

runtimevic avatar runtimevic commented on August 15, 2024

Hello @benhar-dev ,
If it's not too much work for you, I would be grateful if you fork the project and fix it as you think and then make me a PR pull request to see the solution...,
if not I'll fix it...

from oop-iec61131-3--curso-youtube.

runtimevic avatar runtimevic commented on August 15, 2024

Hi @benhar-dev ,
I have documented this issue #13 in the documentation:

If you want to contribute something else?...
When I have time I will fix the error in the Prototype Design Pattern
thanks, Víctor.

from oop-iec61131-3--curso-youtube.

runtimevic avatar runtimevic commented on August 15, 2024

Hi @benhar-dev ,
If you can look at the solution I would be grateful...
https://github.com/runtimevic/OOP-IEC61131-3--Curso-Youtube/blob/master/TC3_Design_Patterns/Prototype/Prototype.tpzip

from oop-iec61131-3--curso-youtube.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.