Some strange error

J

Jim Langston

Ian said:
Your code has a number of memory related nasties, which were too
involved for a casual scan. Run your code under whatever memory
access checker your platform provides and you will find them.

Someone has suggested valgrind. I've never used it myelf, but it seems to
be opensource. I would suggest you go to http://valgrind.org/ and download
it and give it a try. Maybe it'll help.
 
D

david

I won't be able to install any additional software to the machine with
older compiler, I don't have permissions.
I did run the same test with later version of compiler:
-----
As you can see it does not try to free memory twice now.
But I am concerned about other thing, we have code line:
xxx = ccc + ddd;
So it call destructor in assignment operation, that xxx elements would
be freed, there are going to be new ones. This is what I want, but I
can not get why ddd is always freed too. In each line it is freed:
xxx = ccc + ddd;
xxx = ddd + ccc;
xxx = fff + ddd;
xxx = ddd + fff;

Any explation? This is a normal behavior? Does ddd elements even exist
after the first line? Am I playing with the plain memory here already.[/QUOTE]
 
I

Ian Collins

david said:
I won't be able to install any additional software to the machine with
older compiler, I don't have permissions.
I did run the same test with later version of compiler:
-----
As you can see it does not try to free memory twice now.
But I am concerned about other thing, we have code line:
xxx = ccc + ddd;
So it call destructor in assignment operation, that xxx elements would
be freed, there are going to be new ones. This is what I want, but I
can not get why ddd is always freed too. In each line it is freed:
xxx = ccc + ddd;
xxx = ddd + ccc;
xxx = fff + ddd;
xxx = ddd + fff;

Any explation? This is a normal behavior? Does ddd elements even exist
after the first line? Am I playing with the plain memory here already.

A guess from a quick look at your code:

A temporary created by operator+ is used for the assignment to ddd and
then deleted, so the pointers in ddd point to freed memory.

You don't have a copy constructor or an operator=(), so all copies and
assignments will be shallow.
 
D

david

To make sure I made some more test with more detailed information,
everything works fine. xxx = ccc + ddd, first of all it creates
temporary object, then frees elements of xxx and then frees elements
of that temporary. So everything works as it should.

Yes I don't have copy constructor because I did not need it
(operator=() assignment?)

So it looks that I can not do much with the machine using old
compiler, maybe it's the end. It effects only versions and it fires
destructor twice which should not be, might be not mine problem. As I
understood other people had the same or similar problems.
 
I

Ian Collins

david wrote:

Please keep the part of the message you are replying to, your messages
don't make sense without context.
To make sure I made some more test with more detailed information,
everything works fine. xxx = ccc + ddd, first of all it creates
temporary object, then frees elements of xxx and then frees elements
of that temporary. So everything works as it should.

Yes I don't have copy constructor because I did not need it
(operator=() assignment?)

So it looks that I can not do much with the machine using old
compiler, maybe it's the end. It effects only versions and it fires
destructor twice which should not be, might be not mine problem. As I
understood other people had the same or similar problems.

Without a copy constructor, two objects will have the same root pointer.
 
I

Ian Collins

Ian said:
david wrote:

Please keep the part of the message you are replying to, your messages
don't make sense without context.


Without a copy constructor, two objects will have the same root pointer.
I'll follow that up bay saying that any class with a pointer members
should have copy constructor and assignment operator.
 
D

david

I my case it should not, in assignment operator not. First of all I
free all elements from the left side variable (maybe it would better
to call it lvalue?) and then make exact copy of the right variable.
Basically it works as a copy constructor.
 
J

James Kanze

I my case it should not, in assignment operator not. First of all I
free all elements from the left side variable (maybe it would better
to call it lvalue?) and then make exact copy of the right variable.
Basically it works as a copy constructor.

I'm not sure what you're responding to (that "should not"), but
your suggestion as to how to implement an assignment operator
won't work. Think of what will happen if something in the copy
throws.

For classes with pointers to dynamically allocated data, the
swap idiom is simple, and almost always appropriate; if the
class has more than one such pointer, it's really the way to go,
no questions asked. Whatever you do, however, it's generally a
good idea to do anything which can possibly throw before
modifying any member variables; failing that, at least make sure
that calling the destructor will never result in undefined
behavior.
 

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

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,201
Latest member
KourtneyBe

Latest Threads

Top