Re: Very strange problem with Class object



On 31 Mar, 01:09, VK <schools_r...@xxxxxxxxx> wrote:
On Mar 31, 2:53 am, Piotr K <piotr.korzeniew...@xxxxxxxxx> wrote:



On 31 Mar, 00:39, VK <schools_r...@xxxxxxxxx> wrote:

On Mar 31, 2:22 am, Piotr K <piotr.korzeniew...@xxxxxxxxx> wrote:

I simply ran out of ideas why this piece of code works so strange,
it's a very simple class creating object:

var Class = function(tpl) {
return function() {
for(var t in tpl) this[t] = tpl[t];
};

}

var X = new Class({
arr: []

});

var x = new X;
x.arr.push(5);

var y = new X;
alert(y.arr[0]) // displays 5

How, I mean HOW in the world it displays 5? I tested it with normal
string and number variables - works fine, but I can't figure what's
wrong with the arrays? I tried to change the Class function in many
ways like making "new Object(tpl[t])" or "tpl[t].valueOf()" but
nothing helped.. I'm frustrated, it's so freakin strange..

Because you have created a very efficient memory leak over closure
with tpl value retained in it. I honestly have no idea what does this
code suppose to mean. Is it some work-on-denial test for a script
engine? There are more sophisticated professional packages for that,
Acid 3 from the first coming into my mind.

It creates classes, the same way that ie. mooTools does, I just
shortened it for clarity.. however, I managed to solve my problem ;)

You did? OK, enjoy your program working with a memory monitor.

btw. mooTools Class suffers from the same problem which kind a
suprised me.

You mean mooTools cut a deal with the Spirit of Programming so memory
leaks and closures should not be applicable to it no matter how bad
the code is? :-) :-|

My heart is bleeding by looking at this code. Maybe you could take
Prototype.js and its pattern instead? It still imitating class-based
paradigm but it has some respect to the real nature of the used
language so it does not leak 1Mb per 50Kb of code.

Up to you, anyway.

Ups, my mistake, they made it a little diffrent - rather than coping
all attributes they assign them to returned function prototype. So
after change the Class look like this:

var Class = function(tpl) {
var f = function() {
}

f.prototype = tpl;
f.constructor = Class;
return f;
}

I guess this is memory-leek free? :)
.



Relevant Pages

  • Re: MDAC memory leak
    ... when the storage should be freed. ...  Most libraries place the decision of when to free ... strcore leaks are hard to diagnose, because they mean you have an unfreed CString. ... There's a capability of breaking on a particular memory allocation, ...
    (microsoft.public.vc.mfc)
  • Re: MDAC memory leak
    ... when the storage should be freed. ...  Most libraries place the decision of when to free ... strcore leaks are hard to diagnose, because they mean you have an unfreed CString. ... There's a capability of breaking on a particular memory allocation, ...
    (microsoft.public.vc.mfc)
  • Re: tip50 Accepted
    ... This is also the version to be bundled with Tcl core 8.6 beta. ... Is it really true that memory leaks are being plugged in code which is ... It is really unbelievable that a developer would be hunting down ...
    (comp.lang.tcl)
  • Re: tip50 Accepted
    ... This is also the version to be bundled with Tcl core 8.6 beta. ... Is it really true that memory leaks are being plugged in code which is ...
    (comp.lang.tcl)
  • Re: FastMM 4.40 released (replacement memory manager for Delphi IDE and applications, free and open
    ... >> A fast replacement memory manager for Borland Delphi Win32 applications ... >> - Optionally reports memory leaks on program shutdown. ... >> the need for stack frames. ... If exception handling is not in place FastMM ...
    (borland.public.delphi.thirdpartytools.general)