Re: problems with "new"
- From: Charles R <CharlesR@xxxxxxxxxxxxxxxxxxxxxxxxx>
- Date: Thu, 17 Aug 2006 15:02:02 -0700
"Carl Daniel [VC++ MVP]" wrote:
"Charles R" <CharlesR@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:92C2AF98-0BE9-4CAF-80BE-2B7E589F4A35@xxxxxxxxxxxxxxxx
"Doug Harrison [MVP]" wrote:
The obvious question is, "Have you deleted somefoo?" Maybe the real
problem
is that you've deleted an object you expect to still be alive at the time
you allocate buff. BTW, get rid of the cast on new. If you really do need
to cast the result of new (and you don't above), you probably should be
calling the operator new function.
--
Doug Harrison
Visual C++ MVP
I havent deleted somefoo (at least I dont think I have). Below is a code
snippet.
Set a breakpoint on somefoo's destructor (if it doesn't have one, add one).
That'll tell you for sure if you've deleted somewhere.
Also, make sure that there's no possibility that the pointer has been passed
to free() (probably through some convoluted path, if that's what's
happening). For VC++, ::operator new() simply calls malloc, so passing a
new'd pointer to free would "work" (the memory would be freed without
corrupting the heap), but the destructor wouldn't be called. The same would
happen (freed memory but no destructor call) if a call to delete was done on
a void*, or a pointer to a base class that doesn't have a virtual
destructor.
somefoo *gSomefoo;
class somefoo {...}
somefoo* somefoo::GetInstance (CString fooType){
somefoo *medDevice = (somefoo *) new derivedSomefoo (fooType);
}
void InitParams(){
somefoo fooFactory;
gSomefoo = fooFactory.GetInstance(fooType);
}
fooFactory is a local copy in InitParams, and I see that destroyed as
expected. But gSomefoo is global and pointer returned from
fooFactory.GetInstance should be valid. What's happening is later in
code,
another call to new (char *buff = (char*) new char [buffSize]) is
returning
the pointer of gSomefoo and not a new pointer. Just weird.
There are really only two choices:
1. The heap state is corrupted
2. gSomeFoo has been deleted and just happens to be returned by new (or,
indirectly, malloc).
BTW, this is all in a DLL, that happens to be loaded/unloaded regularly.
gSomefoo has to be global instead of a class member variable for a variety
of
reasons.
Also, how come I dont need to cast back to (char *). Thanks.
a new expression always has the type "pointer to T" where T is the type to
the right of the new keyword, not including the outermost array
specification, if any (so in your case, the result of the new expression is
already of type char*).
-cd
I dont have an overloaded operator new defined. Didnt think I needed one,
especially since its going to still call malloc to get memory from the heap.
I think for simplicity, I'll stick to the global new (::new)
I'm only using new & delete, so a call to delete calls the destructor
correct? This is just annoying and I cant figure why/how this pointer is
being deleted.
-C
.
- Follow-Ups:
- Re: problems with "new"
- From: Carl Daniel [VC++ MVP]
- Re: problems with "new"
- References:
- Re: problems with "new"
- From: Doug Harrison [MVP]
- Re: problems with "new"
- From: Carl Daniel [VC++ MVP]
- Re: problems with "new"
- Prev by Date: Re: problems with "new"
- Next by Date: Problems with using namespaces with multiple solution scope
- Previous by thread: Re: problems with "new"
- Next by thread: Re: problems with "new"
- Index(es):
Relevant Pages
|