Doing one last thing to a WeakReference

P

Paul J. Lucas

Daniele said:
Yeah, that's how it goes. Work on a temporary copy if you're concerned
about that.

Needlessly copying around very large images is a non-starter.
By the way, how come you're so much concerned about deleting
a file when your program is actually working on it?

It may be working on it at time T1. At some later time T2 > T1, the user may
decide this image is rubbish and delete it. Users are fickle.
Also, are you sure you open the file in read-only mode?

Windows doesn't care.
Not if you do it properly.

But you may also forget about the timeout, load the file/image metadata
once, and remaining data on-demand.

But I still have to keep the file open in that case.

- Paul
 
S

Stefan Ram

Paul J. Lucas said:
Unfortunately, I don't think Java will ever get
stack-allocation of objects (which would enable true
destructors and RAII) which is what's really needed to solve
the problem at hand.

ISO/IEC 14882:2003(E) does not mention that objects are
allocated from a stack. They have »automatic storage
duration«. An activation record might be implemented as a
stack frame or on the head.

In Java, it is similar: The language does not promise that any
objects are allocated from a stack, but some JVM do an escape
analysis: When the JVM can proof that an object can not
»escape« from the lifetime of a method, it will be allocated
from a stack indeed.

The difference between both languages is that Java does not
have automatic storage duration as part of the language and so
one can not rely on block exit triggering a method.
 
J

John W Kennedy

Stefan said:
C++ has destructors.

Are there any other (object-oriented) programming
languages with destructors?

Turbo Pascal did; I suppose Delphi does. I daresay any OO language that
has either a FREE statement/operator or stack-allocated objects will,
perforce.
 
S

Stefan Ram

Supersedes: <[email protected]>

Paul J. Lucas said:
Unfortunately, I don't think Java will ever get
stack-allocation of objects (which would enable true
destructors and RAII) which is what's really needed to solve
the problem at hand.

ISO/IEC 14882:2003(E) does not mention that objects are
allocated from a stack. They have »automatic storage
duration«. An activation record might be implemented as a
stack frame or on the heap.

In Java, it is similar: The language does not promise that any
objects are allocated from a stack, but some JVM do an escape
analysis: When the JVM can prove that an object can not
»escape« from the lifetime of a method, it will be allocated
from a stack indeed.

The difference between both languages is that Java does not
have automatic storage duration as part of the language and so
one can not rely on block exit triggering a method.
 
J

John B. Matthews

John W Kennedy said:
Turbo Pascal did; I suppose Delphi does. I daresay any OO language that
has either a FREE statement/operator or stack-allocated objects will,
perforce.

As did Wirth's Object Pascal, but I'm not sure free qualifies as a
destructor. In contrast, what the OP wants is a user-defined method that
is called when the storage is actually reclaimed, but at a more
deterministic time than finalize().
 
P

Paul J. Lucas

Lasse said:
Is the variable reference-counted or is the object?

The object (of course).
If the method returns the same reference twice, how is it counted?

It never returns the same *reference* twice. It can, however, return new
references to the same *object* twice.
If it returns a reference to an existing object with other references
out there, how will it be counted?

The count of the object is always incremented when creating a new counted
reference to it. All other facts are irrelevant.

- Paul
 
A

Arne Vajhøj

Stefan said:
Tom Anderson said:
I think the best accomodation that could be made without a major and
potentially awkward addition to the language and runtime system would be a
C#-style 'using' construct:
using (ImageInfo info = getImageInfo(fileName)) {
int w = info.getWidth() ;
// etc
}

In [1], it is described how to write a class
»FileInputStreamOpenCloseAndNull« so that

for( final java.io.FileInputStream fileInputStream:
new FileInputStreamOpenCloseAndNull( "input.txt" ))
use( fileInputStream );

will close »fileInputStream« after the use.

The same can be achieved with a finally.

Arne
 
A

Arne Vajhøj

John said:
Turbo Pascal did; I suppose Delphi does. I daresay any OO language that
has either a FREE statement/operator or stack-allocated objects will,
perforce.

All languages with dynamic allocation and no GC, which probably
is about the same as your criteria.

Ada must fell in this category as well.

Arne
 
A

Arne Vajhøj

John said:
As did Wirth's Object Pascal, but I'm not sure free qualifies as a
destructor. In contrast, what the OP wants is a user-defined method that
is called when the storage is actually reclaimed, but at a more
deterministic time than finalize().

I don't think the argument was that free was a destructor - just that
a language that used free would need a destructor.

Arne
 
J

John B. Matthews

Arne Vajhøj said:
I don't think the argument was that free was a destructor - just that
a language that used free would need a destructor.

Yes, but the utility was apparent only in retrospect. All the languages
mentioned (Ada, C++, D, Delphi, Java, Object Pascal, Turbo Pascal) are
object-oriented and have destructors in the sense of a method that is
automatically invoked when the object's storage is reclaimed.
Conversely, not all object-oriented languages that can reclaim storage
(by freeing dynamically allocated memory or closing a stack frame) have
the method invocation characteristic of destructors. Wirth & Tessler's
original Object Pascal is a familiar counter-example. IIRC, Turbo Pascal
had just a single destructor.
 
P

Paul J. Lucas

Arne said:
Stefan said:
In [1], it is described how to write a class
»FileInputStreamOpenCloseAndNull« so that

for( final java.io.FileInputStream fileInputStream:
new FileInputStreamOpenCloseAndNull( "input.txt" ))
use( fileInputStream );

will close »fileInputStream« after the use.

The same can be achieved with a finally.

Except if "after the use" is not lexically scoped.

- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top