Euphoria Ticket #830: replace() memory leak

There is a memory leak in replace() as demonstrated by the following code:

sequence bar, baz 
while 1 do 
	bar = "a" & "b" 
	baz = bar 
	baz = replace( bar, "c", 2, 2 ) 
end while 

The loop makes the leak easy to observe as it causes the leak to quickly accumulate. The key elements are that bar is non-unique (i.e., reference count greater than 1), the replacement is the same length as the replaced elements.

Details

Type: Bug Report Severity: Blocking Category: Language
Assigned To: unknown Status: Fixed Reported Release: 4.0.5
Fixed in SVN #: View VCS: none Milestone:

1. Comment by mattlewis Jan 17, 2013

See: hg:euphoria/rev/466d30388ab6

changeset: 5944:466d30388ab6 branch: 4.0 tag: tip parent: 5941:6cbb5fa971be user: Matt Lewis date: Thu Jan 17 08:34:32 2013 -0500 files: tests/t_sequence.e description:

  • unit test for memory leak from ticket 830 (currently fails)

2. Comment by jimcbrown Jan 18, 2013

Hmm. I'm not sure how to fix this for real... target and copy_to are identical, but in this case they shouldn't be. We need to come up with some other condition where we can detect a case like this, and force creating a new copy when it happens.

I'm puzzled by this fact however - bar and baz end up with different values (they each are set to the correct values). We must have some other way of distinguishing target and copy_to, otherwise they'd be the same value. (Tested with http://openeuphoria.org/pastey/192.wc )

A simple workaround (we unconditionally create a new copy no matter what), this fixes the ticket but presumbly it makes "foo = replace(foo, ...)" slower.

http://openeuphoria.org/pastey/191.wc

3. Comment by mattlewis Jan 18, 2013

I have a fix...should be pushing up soon.

4. Comment by SDPringle Jan 18, 2013

You can always work around this with the old implementation used before it was decided to make it builtin.

public function replace(sequence target, object replacement, integer start, integer stop=start) 
	target = remove(target, start, stop) 
	return splice(target, replacement, start) 
end function 

5. Comment by mattlewis Jan 18, 2013

See: hg:euphoria/rev/dd834258410e

changeset: 5956:dd834258410e branch: 4.0 parent: 5951:c0fca41995cf user: Matt Lewis date: Fri Jan 18 10:45:20 2013 -0500 files: docs/release/4.0.6.txt source/be_runtime.c description:

  • fix memory leak in replace
  • fixes ticket 830

Search



Quick Links

User menu

Not signed in.

Misc Menu