Home > Archive > VC Language > January 2006 > why the leaking? please advise
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
why the leaking? please advise
|
|
| lallous 2006-01-20, 7:12 pm |
| Hello
I have the following code:
#include <windows.h>
#include <string.h>
#include <tchar.h>
#include <stdio.h>
#include <map>
#include <list>
#include <algorithm>
#include <string>
#include "crtdbg.h"
#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
#define new DEBUG_CLIENTBLOCK
#else
#define DEBUG_CLIENTBLOCK
#endif // _DEBUG
struct B;
typedef std::map<std::string, B> amap_t;
struct A
{
int a;
int b;
};
struct B : A
{
amap_t *ptr;
B(const A &rhs)
{
memcpy(this, &rhs, sizeof(rhs));
}
};
int main()
{
amap_t a;
_CrtMemDumpAllObjectsSince(NULL);
}
When I run it, memory leaks are being reported!!!
Please advise.
/*
Dumping objects ->
{46} normal block at 0x00322F90, 56 bytes long.
Data: < /2 /2 /2 > 90 2F 32 00 90 2F 32 00 90 2F 32 00 CD CD CD CD
Object dump complete.
*/
Is my B's copy constructor correct?
Regards,
Elias
| |
| Jochen Kalmbach [MVP] 2006-01-20, 7:12 pm |
| Hi lallous!
> int main()
> {
> amap_t a;
>
> _CrtMemDumpAllObjectsSince(NULL);
> }
try
int main()
{
{
amap_t a;
}
_CrtMemDumpAllObjectsSince(NULL);
}
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
| |
| Simon Trew 2006-01-20, 7:12 pm |
| "lallous" <lallous@lgwm.org> wrote in message
news:OXhdaqcHGHA.3624@TK2MSFTNGP09.phx.gbl...
> struct B;
> typedef std::map<std::string, B> amap_t;
>
> struct A
> {
> int a;
> int b;
> };
>
> struct B : A
> {
> amap_t *ptr;
>
> B(const A &rhs)
> {
> memcpy(this, &rhs, sizeof(rhs));
> }
> };
>
> Is my B's copy constructor correct?
You should never use memcpy (or placement new) to copy data into class
members. They will not be properly copy-constructed, and you don't know
whether "this" points to the part of B that contains the sub-object from A--
you should at least static_cast<A*>(this) in the call.
But your problem here, I imagine, is that you're just copying the *pointer*
(ptr) rather than the thing that it *points to*. You need to copy the
elements of the map, e.g.:
B(const A &rhs)
{
ptr = new amap_t; // or however you allocated the amap_t before
std::copy(rhs.ptr->begin(), rhs.ptr->end(),
std::insert_iterator(amap_t)); // or:
ptr->insert(ptr->begin(), rhs.ptr->begin(), rhs.ptr->end());
}
| |
| Bruno van Dooren 2006-01-20, 7:12 pm |
| This is not a memory leak. you ask _CrtMemDumpAllObjectsSince to dump all
objects that you created from the start of execution (state == NULL), and
that is exactly what it does. objects were created (your map a was created).
i don't exactly know if your constructor is illegal(though it looks fishy) ,
but this is better IMO
B(const A &rhs) : A(rhs)
{
}
also whithin B, you declared a pointer to a map that maps strings to B
structures. is that what you intended?
kind regards,
Bruno.
"lallous" <lallous@lgwm.org> wrote in message
news:OXhdaqcHGHA.3624@TK2MSFTNGP09.phx.gbl...
> Hello
>
> I have the following code:
>
>
> #include <windows.h>
> #include <string.h>
> #include <tchar.h>
> #include <stdio.h>
> #include <map>
> #include <list>
> #include <algorithm>
> #include <string>
> #include "crtdbg.h"
>
> #ifdef _DEBUG
> #define DEBUG_CLIENTBLOCK new( _CLIENT_BLOCK, __FILE__, __LINE__)
> #define new DEBUG_CLIENTBLOCK
> #else
> #define DEBUG_CLIENTBLOCK
> #endif // _DEBUG
>
>
> struct B;
> typedef std::map<std::string, B> amap_t;
>
> struct A
> {
> int a;
> int b;
> };
>
> struct B : A
> {
> amap_t *ptr;
>
> B(const A &rhs)
> {
> memcpy(this, &rhs, sizeof(rhs));
> }
> };
>
>
> int main()
> {
> amap_t a;
>
> _CrtMemDumpAllObjectsSince(NULL);
>
> }
>
>
> When I run it, memory leaks are being reported!!!
> Please advise.
>
> /*
> Dumping objects ->
>
> {46} normal block at 0x00322F90, 56 bytes long.
>
> Data: < /2 /2 /2 > 90 2F 32 00 90 2F 32 00 90 2F 32 00 CD CD CD CD
>
> Object dump complete.
>
> */
>
> Is my B's copy constructor correct?
>
> Regards,
> Elias
>
| |
|
| "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote
in message news:OqEwyscHGHA.1452@TK2MSFTNGP11.phx.gbl...
> Hi lallous!
>
>
> try
>
> int main()
> {
> {
> amap_t a;
> }
> _CrtMemDumpAllObjectsSince(NULL);
> }
>
> --
> Greetings
> Jochen
>
> My blog about Win32 and .NET
> http://blog.kalmbachnet.de/
Fishy constructors aside, Jochen's solution is correct. In your
(the OP's) original solution, the memory dump is run before your
object goes out of scope. That means memory for it is still
allocated at the time the dump is run.
- Arnie
| |
| lallous 2006-01-20, 7:12 pm |
| The sample I provided was quickly built from a larger code that had a
problem. And in the course of copying, I mis-constructed the sample.
The original code had correct scope / leak detection code placement, but now
it appears that the leak was from something else.
Thanks,
Elias
"Arnie" <none> wrote in message
news:OxdSLJdHGHA.1124@TK2MSFTNGP10.phx.gbl...
> "Jochen Kalmbach [MVP]" <nospam-Jochen.Kalmbach@holzma.de> wrote in
> message news:OqEwyscHGHA.1452@TK2MSFTNGP11.phx.gbl...
>
> Fishy constructors aside, Jochen's solution is correct. In your (the
> OP's) original solution, the memory dump is run before your object goes
> out of scope. That means memory for it is still allocated at the time the
> dump is run.
>
> - Arnie
| |
| Jochen Kalmbach [MVP] 2006-01-20, 7:12 pm |
| Hi lallous!
> The sample I provided was quickly built from a larger code that had a
> problem. And in the course of copying, I mis-constructed the sample.
> The original code had correct scope / leak detection code placement, but now
> it appears that the leak was from something else.
If you want to see the callstack, you could use my leakfinder:
http://www.codeproject.com/tools/leakfinder.asp
--
Greetings
Jochen
My blog about Win32 and .NET
http://blog.kalmbachnet.de/
| |
| lfhyclf@gmail.com 2006-01-21, 7:02 pm |
| Hi Lallous,
I can't see memory leak from the code that you post.
I would recommand you check your application with RuntimeChecker at
http:// www.runtimechecker.com
Just launch your application from RuntimeChecker or attach it to your
running application,
after that, run your scenario that cause memory leak and exit your
application properly
( do not kill it through task manager ).
Regards,
Sophia
support@runtimechecker.com
| |
| lfhyclf@gmail.com 2006-01-21, 9:58 pm |
| Hi Lallous,
I can't see memory leak from the code that you post.
I would recommand you check your application with RuntimeChecker at
http:// www.runtimechecker.com
Just launch your application from RuntimeChecker or attach it to your
running application,
after that, run your scenario that cause memory leak and exit your
application properly
( do not kill it through task manager ).
Regards,
Sophia
support@runtimechecker.com
| |
| lallous 2006-01-23, 4:00 am |
| Thank you all for your responses.
Things are clearer now.
--
Elias
| |
| Ben Voigt 2006-01-26, 7:08 pm |
| > struct B : A
> {
> amap_t *ptr;
>
> B(const A &rhs)
> {
> memcpy(this, &rhs, sizeof(rhs));
> }
> };
[snip]
> Is my B's copy constructor correct?
What copy constructor?
This is a copy constructor:
B::B(const B &rhs);
You have defined a conversion from A to B:
B::B(const A &rhs);
|
|
|
|
|