PDA

View Full Version : Can't delete an object (instance of structure with a nested structure)



tonnot
25th June 2011, 12:15
I have two structures:



struct W1 {int v1;...};
struct W2 {
W1 my_w1;
.....
};

As you can see W2 has a W1 inner member.


I have some functions to do the work :; (pseudo code)
do
Create the instance my_w2 = new w2;
general code to do some things.
delete my_w2;
loop

I have discover that when delete my_w2 keeps my_w1 information ????
How can I delete my_w2 effectively ? What I'm doing bad ?

Also... Is there a way to reset (empty) an object ? (or have I to delete it ?)
Thanks

Santosh Reddy
25th June 2011, 12:20
I have discover that when delete my_w2 keeps my_w1 information ????
What do you mean by "keeps my_w1 information":confused:, how do you know it keeps it? after delete call, my_w2 will be 0

stampede
25th June 2011, 12:39
What else is in W1 structure, maybe something allocated on heap ? In that case, you'll need to free the memory allocated on heap manually (or better - write a destructor for W1 to do this for you).

tonnot
25th June 2011, 15:15
This are my 2 structures:


struct W_perfil_header {
double pk;
unsigned short int num_sups;
double pd,px,py,pz,azi;
};

struct W_perfil {
W_perfil_header header;
std::vector<W_sup> V_sups;
};


the code that work with them :(pseudocode)


class.h
Public :
W_perfil * w_perfil;

class.cpp
create_perf() {
w_perfil = new W_perfil;

do things ....
w_perfil->header.num_sups++;
....

close_perf() {
delete w_perfil; } // I dont know if it is necessary, I have a new at create_perf...



Ok
I suppose that initially
w_perfil->header.num_sups is = 0.
w_perfil->header.num_sups are going to value 1 to 10 ( usually)
I'm surprised that w_perfil->header.num_sups reached 671
( I have 671 calls to create_perf and close_perf ...)

I have search c++`delete on the web and I discover that some people has the same problem as me.... But I keep on with the problem.

I hope you can give some tips...
Thanks

stampede
25th June 2011, 15:31
I suppose that initially
w_perfil->header.num_sups is = 0.
w_perfil->header.num_sups are going to value 1 to 10 ( usually)
I'm surprised that w_perfil->header.num_sups reached 671
( I have 671 calls to create_perf and close_perf ...)
It's hard to tell from the code you posted, because its important what "do things" is doing with your structures. Also we can't say where create_perf and close_perf are called. Why do you think 671 calls to a method is too much ? Are you experiencing a memory leak or something ?

// I dont know if it is necessary, I have a new at create_perf...
You must delete everything allocated with new. If you call it like this:


obj->create_perf();
obj->create_perf();
// without calling close_perf() in between

you have created a memory leak, because previously allocated memory assigned to w_perfil pointer is not released (w_perfil points to new memory block, old pointer value is lost, so you cannot delete it anymore)

Santosh Reddy
26th June 2011, 00:13
I don't see any problem with your code, I mean you don't have a problem, or at-least you are not telling us the actual problem.


I suppose that initially
w_perfil->header.num_sups is = 0.
w_perfil->header.num_sups are going to value 1 to 10 ( usually)
I'm surprised that w_perfil->header.num_sups reached 671
( I have 671 calls to create_perf and close_perf ...)
it does not essentially mean that, it means you have do things .... for 671 times, in a single call of create_perf()

Each time create_perf() is called it start with a new copy of W_perfil, and start with num_sups = 0


// I dont know if it is necessary, I have a new at create_perf...
it is necessary.

tonnot
26th June 2011, 09:21
Thanks.
I think I have found the origin of the problem.
I have not itiallized the default initial values of the members of my structure.
Initializing them, I solve my problem.

Can be this (initializing) the origin of the problem ?
Thanks.

wysota
26th June 2011, 10:01
We have no idea, you still didn't tell us what the problem is. Especially the part about "keeping my_w1 information". After all these posts and our responses you should really learn to express yourself in a clear and informative manner.

Santosh Reddy
26th June 2011, 10:22
As wysota said, no idea. Uninitialized variables can cause (m)any kinds of problems, which can cause nightmares later in the development if not found in early days. It has always been a good practice to have default ctor for a class / struct.

tonnot
26th June 2011, 11:22
Definitively, the problem was initialization.
By any reason, the variables of my structs not initializated was the problem.
Now all worsk fine.

I could not explain why now my prog works, but it works.
When I say that a var was "keeping my_w1 information", I really want to say "remember " (my english, sorry sorry sorry very much ). I think that as I didnt initiallized the vars, the vars remember the last value.

So, my final conclusion is that in any way deleting an object and inmediatelly create can to have no effect. (I have seen some links about it on the web ). Am I right ?

And I final stupid question, why C++ initialized some vars to max values (in example a int to 65535) ???? Or a bool to true ? (Is there any way to give initial value of numeric vars to zero, bools to false)

Thanks, specially for your patience ...

wysota
26th June 2011, 11:33
So, my final conclusion is that in any way deleting an object and inmediatelly create can to have no effect. (I have seen some links about it on the web ). Am I right ?
No, you are wrong.


And I final stupid question, why C++ initialized some vars to max values (in example a int to 65535) ????
C++ doesn't initialize any of your values. They are either random values residing in the chunk of memory where the variable is created or the compiler preinitializes the variables with some known value (often called a "canary") in debug mode. Seeing such value (it's usually multibyte) should make you aware it is related to an uninitialized area of memory.

tonnot
26th June 2011, 11:56
Somebody has tell me at other forum ( about deleting)

The OS merely marks the memory block as free - it does not have to do anything else and certainly it does not have to do anything immediately
So if you do what you did, and immediately try to use the object again, you may find valid values.
So, maybe a delete follow by an inmediatelly new can have no effect ?
Thanks WY

wysota
26th June 2011, 12:26
What do you mean by "no effect"? If you delete an object then its destructor gets called. Then when you create an object then it may be located in a different or the same address in memory and the constructor gets called. If you access an area of memory where an object once was and nothing overwrites this area in the meantime, you'll be reading values that used to belong to the object. If you mean that you can get the same values after reallocating an object, then yes, you can get the same values. If all memory space is preinitialized with zeroes and you don't initialize any memory yourself then every object will contain only zeroes, with or without deleting anything. But they will certainly be different objects.

Tonnot -- programming is not only about knowing syntax of some programming language. It's also, or rather mainly, the knowledge of how computers work, why they work this way and what "physical" result each statement of your program has. It is obvious you are missing all those skills. Do everyone (yourself included) a favour and read a good book on computers and spend an hour a day on the Web reading and understanding how computers work. And I don't mean asking on forums but reading papers and books on computer science. You will save yourself a lot of time and nerves this way. And investing in a couple of English lessons (especially grammar) would be a good thing to do as well, sometimes it is very hard to understand what you want.