|
Thanks for some reason I was thinking it "destroyed" everything. So only the pointers are deleted but not the actual value stored in memory. So if these weren't pointers then we would have a "memory" leak problem? Say for instance we remove the CWnd, but keep int and long we call delete would those values be removed from memory?
Sam C
----
Systems Manager
Hospitality Marketing Associates
|
|
|
|
|
You're almost there!
Yes, when 'delete' is called on the struct/class, the members are destroyed. If those members are pointers, then the pointer is destroyed, but not what is pointed to. If you have no other pointer to the 'pointed to' object, then you have a memory leak (remember - you can have multiple pointers pointing to the same memory address/object).
If you want the things that are 'pointed to' to be destroyed when you call 'delete', you write a destructor that does this. As a general rule (there are always exceptions, and the code you are looking at is one of those exceptions)
if a class/struct contains pointers, it should also have a destructor that 'cleans up' the pointed to objects. And a copy constructor and an assignment operator - but that's getting a little too complicated for now!
So far, so good (I hope).
If the members are just 'builtin types' like int, then when delete is called the int is destroyed. However, in your code you copied the int value from the struct just before calling delete. Therefore, the 'int' in the struct is removed from memory by the call to delete, but the copy you made still remains valid, becuase it is a different copy of the int value from the struct. Er... I think that's what I'm trying to say!
-----------------------
Reg : "Well, what Jesus blatantly fails to appreciate is that it's the meek who are the problem."
|
|
|
|
|
I think the easy way to get your head around pointers is to think of them as addresses. the RAM (memory) is a street, and every house on that street has an address (a pointer). If you want to tell your friend to go to a certain house, you write the address on a piece of paper, and you give him the paper (you have just passed a pointer, in a customstruct(on paper)), then he goes out and meets another friend, who also wants the addresses that he writes in his notebook (gets a copy of the pointer). Now your friend decides he doesn't need the address any more so he throws away the paper (deletes the custom struct, but the memory(house) still exists). As long as the house exists the address is valid, but if the house burns down (your object is deleted from memory) the address will point to an empty lot (invalid memory).
I hope this helps, this analogy helped me understand pointers
---
Blessed are those who can laugh at themselves, for they shall never cease to be amused
|
|
|
|
|
Actually this is a very good analogy!!! I GET IT (somewhat) NOW! Thanks for that analogy I really appreciate it.
Sam C
----
Systems Manager
Hospitality Marketing Associates
|
|
|
|
|
Just wanted to give everyone a blanket THANKS. I finally am getting a good grasp of everything that is being said about pointers. I thought I had a good grasp before, but that creating a structure and then deleting it in another function kind of killed me
But thanks to all for being patient and taking the time to help me understand this one concept better.
Sam C
----
Systems Manager
Hospitality Marketing Associates
|
|
|
|
|
I created by Wizard ATL COM DLL that import my MFC extention DLL(from type: base Dialog) in my ATL.
Please check my steps (describes below) and tell me if I miss something.
(because when I try to perform its function from client program it become crazy)
------------------------------------------------------------------------
1. I opened project: ATL COM Wizard. [DLL] [MFC Support]
2. I inserted to the library that created the file : MyMfcDll.dll
and attached to the ATL project the files : MyMfcDll.h , MyMfcDll.lib
3. I Added by wizard ATL class [MyClass] [single] [dual]
4. I added by wizard method [ShowMfcDialog] to IMyClass and fill it so :
___________________________________________
#include "MyMfcDll.h"
STDMETHODIMP CMyClass::ShowMfcDialog()
{
AFX_MANAGE_STATE(AfxGetStaticModuleState())
CMyDialog dlg; //from the imported dll
dlg.DoModal();
return S_OK;
}
______________________________________________
5. I registered the dll (regsvr32)
and that's all !
What is missing ?
I will be greatful if you will help me ! PLEASE !!!!!
|
|
|
|
|
I'm trying to find a good article on how to use the CArray. I have never used CArray and would like to learn about it and implement it. Any one have a link to a good "How to" use CArrays?
Thanks,
Rob
|
|
|
|
|
Rob, CArray is like CList, but the principal diferens is that In CArray you Use Index and not Position like in CList, For me is more easy work with CArray.
If you need help, I'm here
Cheers;)
Carlos Antollini.
|
|
|
|
|
Thanks Carlos!
I have a app that uses a List control.. I get and send WM_COPYDATA messages to and from other apps. At that point I validate if that item is or is not all ready in the list control if it is I don't add it and if it's not I add it.. I then can minimize, maximize, close or do a ton of other functions using the list control..
After implementing all these different functions in the list I am starting to find that everything is getting super complicated in keeping track of whats in the list, what should be added or removed and what fields to change if something needs to be changed or updated..(I need to do validation on every cell and update needed cells with-out adding another entry) I have other people suggesting that I should use an array to keep track of everything and use the list to just view the data..
I have never used the CArray and don't even know if it's what I should use in my situation.. Any suggestions?
Rob
|
|
|
|
|
I agree with You.
I will give you a simple help.
For example:
The constructor of CArray is:
CArray<TYPE, TYPE>
Never forgot #include <afxtempl.h>
You can define
CArray<int,int> ptArray;
Or
CArray<MyClass, MyClass> ptArray;
ptArray.Add(20); //Add 20 as element 0
ptArray.Add(15); //Add 15 as element 1 in the array
ptArray.InsertAt(0, 23)); // New element 0
int num = ptArray.GetAt(1); //returns the element 1
int nSize = trArray.GetSize(); //returns the number of elements in the array.
you can append a array to another array
for example.
CArray<int, int> oArray;
oArray.Append(ptArray);
with removeAll you can delete all elements in the array.
ptArray.RemoveAll();
with RemoveAt() you can remove n elements in the array.
RemoveAt(nIndex, num)
ptArray.RemoveAt(1, 2); //remove starting from the element 1, two elements.
You Understand????
Best Regards
Carlos Antollini.
|
|
|
|
|
I agree with You.
I will give you a simple help.
For example:
The constructor of CArray is:
CArray<TYPE, TYPE>
Never forgot #include <afxtempl.h>
You can define
CArray<int,int> ptArray;
Or
CArray<MyClass, MyClass> ptArray;
ptArray.Add(20); //Add 20 as element 0
ptArray.Add(15); //Add 15 as element 1 in the array
ptArray.InsertAt(0, 23)); // New element 0
int num = ptArray.GetAt(1); //returns the element 1
int nSize = trArray.GetSize(); //returns the number of elements in the array.
you can append a array to another array
for example.
CArray<int, int> oArray;
oArray.Append(ptArray);
with removeAll you can delete all elements in the array.
ptArray.RemoveAll();
with RemoveAt() you can remove n elements in the array.
RemoveAt(nIndex, num)
ptArray.RemoveAt(1, 2); //remove starting from the element 1, two elements.
You Understand????
Best Regards
Carlos Antollini.
Carlos Antollini.
|
|
|
|
|
Yes I understand. Thanks! It looks alot like a list control. I'll play around with it.
Thanks for all your help!
Rob
|
|
|
|
|
I take it you've had CArray explained to you sufficiently, I'll only add that it is better to use vector from the STL for a myriad of reasons.
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
Thanks Christian,
I looked into using stl vector a few weeks back, but it looks to be much more complicated to use.. at least for what I am trying to do. I think I'll stick with the CList in this project but I am defiantly going to study up on both the CArrays and STL Vectors so I can use either in a future project..
Thanks again!
Rob
|
|
|
|
|
STL *looks* complex at first, but it really isn't. By all means stick with what you know if you need to get something done, but it will definatley be worthwhile to investigate STL when you have time. Plenty of people here to ask if you get stuck.
Christian
As I learn the innermost secrets of the around me, they reward me in many ways to keep quiet.
Men with pierced ears are better prepared for marriage. They've experienced pain and bought Jewellery.
|
|
|
|
|
I'd "second" Christian's advice on STL std::vector (and std::list) - they are well worth the time to learn, are not actually all that complex, and will lead you into a whole range of new coding techniques and possibilities.
-----------------------
Reg : "Well, what Jesus blatantly fails to appreciate is that it's the meek who are the problem."
|
|
|
|
|
Hi
I have a problem :
When I finished my software in Visual C++ (It is about Winsock and ODBC) and it runs very well on my PC, I saw that in others PC that they haven't installed Visual Studio the software doesn't run.
I compilled my software like a static library but it doesn't run too
My question is How I can run my software in other PC then they haven't installed the Visual Studio?.
Thanks
--
Agusti Baubi
|
|
|
|
|
You need to Know what files do you need, for example:
If you are using MFC you need to distribuit the msvcrt.dll and mfc42.dll, if ypu are using odbc also you need to distribuit the odbc librarys, if that pc have not odbc, etc.
Best Regards....
Carlos Antollini.
|
|
|
|
|
Hello All,
I am creating a MFC Extension DLL (not regular DLL) to export MFC classes and checked the Automation option. However, the generated code is not sufficient to function as Automation client.
The DllGetClassObject/DllCanUnloadNow etc codes are not generated. Do I have to manually add this? If yes, what are the other necessary steps to take to conform to the MFC Automation structure? (for instance the regular DLL calls the COleObjectFactory::RegisterAll() in the CWndApp::InitInstance() method. Do I have to do something similar?)
Best regards,
Paul.
Paul Selormey, Bsc (Elect Eng), MSc (Mobile Communication) is currently Windows open source developer in Japan, and open for programming contract anywhere!
|
|
|
|
|
could any body help me in parsing Command Line argument. iwant to send DWORD ,DWORD, STRING. in form of "/5676/6766/test/" . is there any better way of doing that.
anshuman
anshuman
|
|
|
|
|
|
|
Do you know any class that can put a BMP as a background in a window??
I know that CpictureWindow here on CodeProject but I don't handle it!
Do you know a class that can do that!??
Nikado
|
|
|
|
|
What problems do you have with CPictureWindow
Do you want to tile this bitmap across the window, stretch the bitmap to fit the window or just paint it in the centre?
Michael
|
|
|
|
|
Try this
http://www.codeproject.com/docview/mditab.asp
A bit more than what you need but you can exact what you need from it.
Best regards,
Paul.
Paul Selormey, Bsc (Elect Eng), MSc (Mobile Communication) is currently Windows open source developer in Japan, and open for programming contract anywhere!
|
|
|
|