overcoming a dealocating memory problem ?

J

Jole

Hi
I'm using serialization to read in an object from a file. My code basically
does this:

1)MyClass obj = (MyClass) in.readObject();

('in' is an ObjectInputStream wrapped around a FileInputStream object, ie
the code reads an object stored in a file on disk)

Now, what happens is my code does this once at the start (works fine), but
later i need to do the same thing, to read in an updated version of the
object. ie, i need to do this again:

2) obj = (MyClass) in.readObject();


the compiler tells me that obj is constant and can't be assigned again. I
was hoping i could do something like this at step 2:

delete obj; //free memory
obj = (MyClass) in.readObject(); //read in new version of object from file
//and assign it to the same reference

(don't pay attention to the syntax, but the concepts..ie the above frees the
associated memory and then reads in a new copy from the file and assigns it
to the reference obj)

There doesn't seem to be a way to do it. Any ideas?

thanks
Jole
 
G

Gordon Beaton

I'm using serialization to read in an object from a file. My code
basically does this:

1)MyClass obj = (MyClass) in.readObject();
[...]

2) obj = (MyClass) in.readObject();
the compiler tells me that obj is constant and can't be assigned
again.

How *exactly* did you declare obj? Did you use "final"? Don't!

In general, you'll get better help if you post real code, instead of
what your code "basically" does, since in most cases whatever is
causing the error doesn't get posted when you summarize like that.

/gordon
 
M

Michael Borgwardt

Jole said:
2) obj = (MyClass) in.readObject();


the compiler tells me that obj is constant and can't be assigned again. I
was hoping i could do something like this at step 2:

delete obj; //free memory
obj = (MyClass) in.readObject(); //read in new version of object from file
//and assign it to the same reference

Your problem has nothing to do with allocation of memory. The only case where the
compiler wouldn't allow you assign to the variable again is if it's declared as
"final". Remove that keyword from the variable declaration and it should work.

You seem to be completely unfamiliar with the way memory management in Java works.
It uses "garbage collection". Memory is allocated when an object is created via
"new" (or cloned, or deserialized) and deallocated automatically when there are
no more references to it.

You have a potential problem in your code because ObjectInputStream retains
references to all objects read from it, so their memory can't be reclaimed
as long as the stream is not closed or reset.
 
T

Tony Morris

Jole said:
Hi
I'm using serialization to read in an object from a file. My code basically
does this:

1)MyClass obj = (MyClass) in.readObject();

('in' is an ObjectInputStream wrapped around a FileInputStream object, ie
the code reads an object stored in a file on disk)

Now, what happens is my code does this once at the start (works fine), but
later i need to do the same thing, to read in an updated version of the
object. ie, i need to do this again:

2) obj = (MyClass) in.readObject();


the compiler tells me that obj is constant and can't be assigned again. I
was hoping i could do something like this at step 2:

delete obj; //free memory
obj = (MyClass) in.readObject(); //read in new version of object from file
//and assign it to the same reference

(don't pay attention to the syntax, but the concepts..ie the above frees the
associated memory and then reads in a new copy from the file and assigns it
to the reference obj)

There doesn't seem to be a way to do it. Any ideas?

Since you clearly lack fundamental understanding of the technology that you
are using (Java), two questions arise:

a) ummm... what ? You have provided a problem statement that has been
constructed in an ad hoc fashion, which is erroneous and thus, contains no
valuable description of your actual problem. One can only speculate at what
your actual problem (the best guess at the moment is the use of the modifier
'final'). Sometimes "you don't know what you don't know"; that is, you'd
describe it better if you could, but due to a lack of understanding, you
can't. In this case, it is best to provide a working code sample, that
compiles (so that more experienced people can reproduce your problem), and
also contains your expected output and the actual output. That way, your
problem is communicated clearly (by the code), and so is your
misunderstanding (your expected outcome).

b) Why are you using a technology in such a way (i.e. Serialization) that
requires at least a fundamental understanding the concepts involved ?
Sometimes it is best to just drop your pride, accept your shortcomings and
start back at the beginning.
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/index.html

--
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)
 
J

Jole

Hi all,
Sorry if i've upset everyone. I'm still looking at this problem, but i may
have the answer. That code i wrote was a summary of it.

here is the real:

//Get_New() member function of class MyClass
File ListFile2 = new File("list2");
FileInputStream fis2 = new FileInputStream(ListFile2);
ObjectInputStream inn = new ObjectInputStream(fis2);
this = (MyClass) inn.readObject();
inn.close();
//end member function

the compiler says:
cannot assign a value to final variable this..

Okay, i know what yous are going to say, 'it's dead easy, and my code is
different from my previous summary code'. yes, but b4 my summary code
produced the same error. Although now it doesn't . I'm still looking into
why this is the case, as it didn't work b4. Anyhow, it seems that you
can't use 'this' to make the object point to another one newly created in
memory. But you have to use this normal syntax form only:

old_object_name = (MyClass) inn.readObject();


anyway, thanks again.
J
 
J

John C. Bollinger

Jole said:
here is the real:

//Get_New() member function of class MyClass
File ListFile2 = new File("list2");
FileInputStream fis2 = new FileInputStream(ListFile2);
ObjectInputStream inn = new ObjectInputStream(fis2);
this = (MyClass) inn.readObject();
inn.close();
//end member function

the compiler says:
cannot assign a value to final variable this..

And the compiler is exactly right. The compiler message may be a bit
confusing because "this" is not really a variable at all, but the
compiler is quite right that you can never assign to "this". (The
"this" variable refers to the object on which the current method is
operating -- it doesn't even make sense to try to assign to it.)
Okay, i know what yous are going to say, 'it's dead easy, and my code is
different from my previous summary code'. yes, but b4 my summary code
produced the same error.

Which then told you exactly what the problem was ("cannot assign to
Although now it doesn't .

On yet again different code, apparently.
I'm still looking into
why this is the case, as it didn't work b4. Anyhow, it seems that you
can't use 'this' to make the object point to another one newly created in
memory.

Answer one or more of the following:
(1) What do you even mean by that?
(2) Why would you think there was any chance of it working?
(3) If it somehow did work, what would the result be?
But you have to use this normal syntax form only:

old_object_name = (MyClass) inn.readObject();

() That is the only syntax available for assigning object references to
variables.
() Variables are the only program entities that can be assigned to.
() Final variables can only be assigned to once, and must provably be
assigned before their value is ever used.
() "this" is not a variable at all, but if it were one it would
certainly be final.

Go learn Java if you plan to continue using it.


John Bollinger
(e-mail address removed)


P.S.: comp.lang.java does not exist. Do not post there, and especially
do not direct followups there. When you do direct followups to a
different group, advise that you are doing so. -- JCB
 
J

Jole

John said:
And the compiler is exactly right. The compiler message may be a bit
confusing because "this" is not really a variable at all, but the
compiler is quite right that you can never assign to "this". (The
"this" variable refers to the object on which the current method is
operating -- it doesn't even make sense to try to assign to it.)


Which then told you exactly what the problem was ("cannot assign to


On yet again different code, apparently.


Answer one or more of the following:
(1) What do you even mean by that?
(2) Why would you think there was any chance of it working?
(3) If it somehow did work, what would the result be?


hi..what i was trying to do was to update that object through a member
function. so instead of doing:
object = (MyClass) inn.readObject();
i wanted a MyClass.Get_New() member function to do it. So i thought of
using 'this' (as it's a reference to the object in memory), and assigning
it to another newer copy read from a file. Anyway, turns out 'this' is
constant so you can't do it. I'll have to use normal object references
instead.
 
M

Michael Borgwardt

Jole said:
hi..what i was trying to do was to update that object through a member
function. so instead of doing:
object = (MyClass) inn.readObject();
i wanted a MyClass.Get_New() member function to do it. So i thought of
using 'this' (as it's a reference to the object in memory), and assigning
it to another newer copy read from a file. Anyway, turns out 'this' is
constant so you can't do it. I'll have to use normal object references
instead.

You might be able to get the same results by replacing all the fields of
the object with those of the new one.
 

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,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top