Comments (6)
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.
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.
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:
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- 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.
But why is the destructor called at all?
from perl5.
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.
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)
- Segmentation fault in Perl_csighandler3 when used XS with threads. HOT 10
- BBC: Blead Breaks File::Tabular HOT 10
- feature 'class': out of memory error if subclassing a class with an explicit new HOT 3
- t/op/try.t: one test fails during deparsing tests HOT 3
- strftime crashes with a blessed format HOT 3
- [doc] Bug in Perl Threads tutorial section "A Thread Pitfall: Deadlocks"
- BBC: Blead Breaks Mojolicious HOT 3
- Cross compilation HOT 2
- BBC: Blead Breaks CPAN HOT 4
- Class methods can still be replaced outside the class HOT 10
- BBC: Blead Breaks DBIx::Class HOT 2
- Extensive CPAN breakage from two code removal projects HOT 6
- Perl is always configured for AFS on RHEL9 and derivatives
- 0351a629e7 led to build-time failure in Class-MethodCache
- CPAN distributions broken by switch removal HOT 3
- BBC: Scalar-List-Utils-1.65 breaks Tie::RefHash::Weak HOT 10
- Deprecation of GIMME long promised but not yet scheduled
- Script run does not always confine digits to the same set of ten HOT 1
- goto &SUB segfaults with builtin::refaddr, reftype, stringify, etc
- Some closures leak memory in v5.40 HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from perl5.