Giter Club home page Giter Club logo

Comments (6)

mauke avatar mauke commented on August 28, 2024 1

I do get the Required parameter 'dependency' is missing for "Foo" constructor error, but then DESTROY is called anyway and segfaults. I think the issue is that the destructor is run at all for objects whose constructor hasn't finished successfully.

from perl5.

jbarrett avatar jbarrett commented on August 28, 2024

Good spot, I've amended the report.

@ilmari also pointed out, which might be useful for those looking for this type of lifecycle management, that spelling it method DESTROY will allow access to class members directly.

from perl5.

leonerd avatar leonerd commented on August 28, 2024

Building a debug perl yields this:

$ ./perl -Ilib gh22278.pl 
Required parameter 'dependency' is missing for "Foo" constructor at gh22278.pl line 14.
perl: class.c:299: Perl_pp_methstart: Assertion `fieldp[fieldix]' failed.
Aborted (core dumped)

Indeed the issue is that the object isn't fully-constructed at this point, so some of its SV pointers in the fields array are still NULL.

There's two potential approaches to fixing this:

  1. pp_methstart can be more defensive and skip over fields whose pointers are still NULL. This would incur a (very) slight performance cost at every method entry point, having to check all those fields, though I suspect it's small measurement noise at that point, or
  2. Instance construction could be split into two phases. The first phase would populate every scalar/array/hash field with a fresh but empty container variable, before actually running any of the initialisation code. That would involve walking the entire ancestry twice during construction.

Overall I feel that option 1 is probably better here, as it involves far smaller code change. A single extra NULL pointer comparison is unlikely to make a huge impact as compared to making all constructors quite a bit slower.

from perl5.

mauke avatar mauke commented on August 28, 2024

But why is the destructor called at all?

from perl5.

leonerd avatar leonerd commented on August 28, 2024

But why is the destructor called at all?

Perl calls sub DESTROY when it throws away an object instance. There's not a lot we can do about that here.

Certainly we could create (and encourage folks to use) a separate DESTRUCT phaser that is a phaser, not a method, and ensure each one only gets invoked after that particular part of the constructor has completed successfully. In most cases that would be what you wanted to use. But aside from far deeper changes elsewhere, we can't do much about DESTROY so we have to be prepared to handle it.

from perl5.

leonerd avatar leonerd commented on August 28, 2024

It seems the wider topic of how destructors ought to work is still a thing that needs discussing, though I don't think this bug is the place to do it. I'd suggest we begin a separate discussion about that to look at the wider topic overall.

from perl5.

Related Issues (20)

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.