MFC CObArray implemetation problem

Help me please!
Sorry for my english.
I do not understand a thing about CObarray.

Test code.
File.h
CObArray m_arr; // member variable.

File.cpp

CPersona *p = new CPersona(nome, cognome);

m_arr.Add(p);

delete p;

CPersona *p2 = m_arr.GetAt(0);
CString sz = p2->GetNome();

If i use “delete” as in the code, sz error runtime.
But if i
// delete p;
Everything works except obvious memory leaks.

Where accident concept?

What sense does create an CObArray of CPersona if when i delete it my CObArray is unusable??!!

We might as well create an array of CPersona like

CPersona n_arr[MAX]

without using CObArray, or not?

where mistake?

Thanks.
What sense does create an CObArray of CPersona if when i delete it my CObArray is unusable??!!

Of course you have to delete your objects, but at the right time i.e. when the program finishs.

We might as well create an array of CPersona like
CPersona n_arr[MAX]


Sure you can but the size of the array is fixed.

I think a better solution is to use a std::vector<CPersona> without pointers
closed account (48T7M4Gy)
I get the impression you are confusing MFC object arrays via CObArray with c-style arrays/new and delete etc.

Just use the CObArray's in accordance with Microsoft usage below.

( You are might be better off just using STL <array> containers if you aren't locked into using MFC )

https://msdn.microsoft.com/en-us/library/088sck34.aspx
https://msdn.microsoft.com/en-us/library/7y3w7f0d.aspx
https://msdn.microsoft.com/en-us/library/c88bb7t9.aspx
Thank you to all of you who have responded to your patience...
I'm an old new "programmer" and yes, i'm confused because i can't explain.

I would keep the objects in an array (member variable) from which to recover when I need them . But if I do not delete objects CPersona why do I need to create a CObArray ?? Imight as well keep the objects without creating the array

Thanks
closed account (48T7M4Gy)
An array of any kind is a form of container to add/store/remove and retrieve objects in an orderly way in just the same way as a library is an array of books.

So from that you can see it is possible to have books with or without a library. The choice is yours not a fault/inadequacy/waste of time with either.

Also the action of placing an object in an array and then removing it does not constitute deleting it.
When a CObArray object is deleted, or when its elements are removed, only the CObject pointers are removed, not the objects they reference.
I think I understand...
CObArray.Add add a only pointer to the object do not a copy. Then if i delete the object this pointer falls.

As the list of books in a library.

An example of what I would :
A friend loaned me a book

Book book = new Book();

I'll copy and put it in my library

myArr.add(book);

and then back to the original book to friend

delete book;

keeping a copy in my library for read it when I want to

CBook *b = myArr.GetAt(x).
b->Read();

Is correct, true?

Thank You


closed account (48T7M4Gy)
You need to:

Follow the examples given above. Experiment. Write some code. Test it. Correct it. Write executable code instead of just writing notes.

delete book is not relevant - delete is not a CObArray method. delete is associated with new.

Similarly Read() is a CBook method and has nothing to do with myArr

You need to separate CBooks, new/delete and CObArray as three separate concepts.
Thanks.
Topic archived. No new replies allowed.