Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number
 
PerlMonks  

Re: method chaining fails where separate method calls succeed in DBIx::Simple

by thenaz (Beadle)
on Aug 04, 2011 at 18:08 UTC ( #918614=note: print w/ replies, xml ) Need Help??


in reply to method chaining fails where separate method calls succeed in DBIx::Simple

I'm guessing that the reason it is important to not increase the reference count, is that the database object keeps references to its active statements. So if the database object references its statements, and the statements reference the database object, you have a circularity, which breaks Perl's reference-counting GC scheme (you'll have a memory leak).


Comment on Re: method chaining fails where separate method calls succeed in DBIx::Simple
Re^2: method chaining fails where separate method calls succeed in DBIx::Simple
by Eliya (Vicar) on Aug 04, 2011 at 18:28 UTC
    ...you have a circularity, which breaks Perl's reference-counting GC scheme

    But isn't this what Scalar::Util::weaken() is for — as (somewhat indirectly) hinted at in the OP ?

    (Though I suspect that what Juerd meant with "it's safer to just have a string" is that if you take a copy of a weak ref, the copy will be strong again, which could potentially lead to the problem you mention...)

    PS: I'm not the original poster.

Re^2: method chaining fails where separate method calls succeed in DBIx::Simple (XS mortals)
by tye (Cardinal) on Aug 04, 2011 at 18:37 UTC

    Somewhat more specifically, you want to know when the statement handle is no longer being used so that it can clean itself up (and perhaps warn that you left it in an unfinished state, probably indicating a bug).

    The described bug that is prevented by removing the quoting sounds exactly like somebody has a bug in their ref count management in their XS code. It sounds like somebody has marked a variable as "mortal" and so the subsequent method call decrements the variable's ref count, destroying it even though an attempt is then made to use it. The destruction doesn't happen if the returned blessed reference is saved into a named variable.

    Have fun finding the ref counting bug in the XS code. It is rarely even close to an easy task. On the other hand, introducing such bugs is extremely easy in XS code and is probably one of the major contributors to the rather frequent problems I see with "weird bugs" that just "go away" when some XS module stops being used (even though the XS module doesn't even seem related to the "weird bug").

    I wish more XS authors learned to only manipulate Perl data structures from Perl code, strictly minimizing their XS code. Or, usually better, just skip the XS code altogether.

    - tye        

        There is no XS code involved

        Uh, I bet DBI is "involved" which usually means quite a large amount of XS code. Moose is mentioned which pulls in a ton of other modules, several of which involve XS code.

        And I already noted that XS bugs have a tendency (IMO, IME) to impact things that don't appear at all related:

        probably one of the major contributors to the rather frequent problems I see with "weird bugs" that just "go away" when some XS module stops being used (even though the XS module doesn't even seem related to the "weird bug").

        (emphasis added)

        it makes no sense to have the quoting in the first place

        Several people appear to disagree with you on that point. If you want to argue against them, then perhaps you should address their points rather than throw out just "it makes no sense" (to you) and "it's unorthodox" (in your experience).

        Several objects are being used as keys to hashes. It is very similar to "inside out object" techniques. Yes, "inside out objects" were rather unorthodox until they became rather mainstream and even a widely adopted "best practice" (and have fallen out of favor a bit since, IME).

        So I don't find it unacceptable to keep this key around. Nor do I object to avoiding the creation of circular references (or even just avoiding some risk of circular references). Hash keys are just strings so "$self" is how you get the hash key.

        And it CAUSES a bug.

        Well, for some less useful definitions of "causes", I guess. It certainly isn't the root cause of the bug, IMO. Based on the somewhat vague description of the bug, it sounds to me like a bug that should be impossible to create just in Perl code.

        It should be impossible for one to write Perl code such that

        my $x= get(); my $y= $x->method(); # vs. my $z= get()->method();

        produce different results.

        So I stand by my assertion that there must be a real bug in some non-Perl code (either XS code or Perl's own C code -- XS code being the much more likely case) for this to happen (though the problem statement is vague enough that I could easily be misunderstanding).

        So it would be good for somebody to do some more debugging in hopes of finding or at least narrowing down the location of this other bug.

        The code of DBIx::Simple is complex enough that I was not able to quickly understand the interplay of object life cycles. I have no confidence that changing "$self" to $self won't "CAUSE" bugs for other users of that module.

        So it would be good to provide some complete, stand-alone code that reproduces this problem so it can at least be added to the test suite for the module (probably marked "to-do" for now).

        If somebody makes the effort to understand the interplay of object life cycles, then it would be good to add some test cases around such things as well. Then we might even be able to point at a specific case that would be broken by the proposed change. Or be able to point at the tests around how life cycles are supposed to interact and how the proposed change doesn't break any of those tests.

        - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://918614]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (5)
As of 2014-09-23 01:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (210 votes), past polls