Re: Problem in performance of calling a dialog in DLL(Windows prog
"creative22" <creative22@discussions.microsoft.com> ha scritto nel messaggio
news:76C6009A-40D6-4FC1-B7C3-532B8A55399E@microsoft.com...
I really don't know how should I Thank you!!
You are welcome.
your code is complete and better of mine,but I would ask some questions of
my own code from you;
My problem Solved with removing static part of p declaration!
OK.
And about Memory leaks:
static buff* pad = new buff;
I agree with you Tom that this line is wrong
The reason of static definition of pad is that I want to keep its last
value!
The use of 'static' in that context is good, because you want to preserve
the value of 'pad' in successive calls to that function.
The problem was the initialization with allocation 'new buff'.
You should have done simply:
static buff * pad = NULL;
And then store the pointer passed in WM_INITDIALOG switch case, like I did
in my rewrite in my previous post:
[...]
// Save buffer pointer
buff = reinterpret_cast<Buff *>( lParam );
Yes, youre right!
I still have many problems in memory allocations and freeing of C++
Could you introduce me any Excellent and Comprehensive reference of
memory
allocations and freeing of C++ topic?
And What section of MSDN library do you suggest me to study well?
I learnt C++ several years ago, coming from C, and before from assembly of
Commodore 64.
I just studied some C++ books like "Thinking in C++" by Bruce Eckel; there
was a free downloadable version available some years ago. You may want to
use your favourite search engine to find it, assuming that it is still
available.
You should focus your study on concepts like stack vs. heap, and
'new/delete'.
I'm not aware of any online tutorial, but I believe that if you search the
web you will find lots of information.
And the most important thing is to write code, and learn from errors. I
think that everyone of us followed this path of learning by errors.
Basically, you should have in your mind that when you allocate something
using 'new' on the heap, the same pointer must be freed using 'delete' in
some point in your code.
Modern C++ offers useful classes like smart pointers like shared_ptr, or
vector containers like std::vector, or string classes like CString and
std::[w]string that make your life easier and your code simpler.
Moreover, he would benefit from using some C++ utility >class like
CString, instead of raw TCHAR arrays
Yes, its a good case,but I would try TCHAR arrays,because in "Win32" we
don't have "CString" or generally "Class" concept!
And my dll is written in "Win32" and can't use of "CString", my MFC code
just calls the Secondfunction() of dll!
You can use STL classes like std::string or std::wstring in your Win32 C++
code.
And you can use CString, because since VS2003 it was factored out from MFC,
and put in ATL.
So, basically, CString can be used also in pure C++ non-MFC projects, like
your DLL.
These lines added to detect memory leaks:
#define _CRTDBG_MAP_ALLOC
#include <stdlib.h>
#include <crtdbg.h>
_CrtDumpMemoryLeaks();
And the result after debugging was such below:
Detected memory leaks!
Dumping objects ->
{142} normal block at 0x003898F8, 92 bytes long.
[...]
But these lines dont show the code line in which memory leak has occurred
when I double click them(as described in MSDN)!!
Please read the link I posted you in some other message in this thread:
"How to: Set Breakpoints on a Memory Allocation Number"
http://msdn.microsoft.com/en-us/library/w2fhc9a3.aspx
I also read "How to: Set Breakpoints on a Memory Allocation Number"
On MSDN,but couldnt set Breakpoints because the debugger didnt recognize
_crtBreakAlloc or {,,msvcr71d.dll}_ crtBreakAlloc in the Watch
Window!!
Could I ask you how found the code lines related to memory leaks?
The problem is that if you use VS2008, you should replace msvcr71d.dll with
msvcr90d.dll (you are right: this is a documentation bug).
Then, when the debugger breaks on memory block, you can use the call stack
to check which part of your code allocated that block, and become aware of
the offending code.
And Finally:
Do you have any other suggestion and modification to this code regarding
my
purpose?!
I posted you a rewrite of your code, using modern C++ techniques like
container classes, string classes, etc.
You may want to consider it as a possible solution. (Note that that code is
simpler than your code, thanks to the use of proper useful C++ classes.)
Thanks a lot,
You are welcome,
Giovanni