You are viewing a plain text version of this content. The canonical link for it is here.
Posted to c-dev@xerces.apache.org by Sam Lindley <sa...@redsnapper.net> on 2004/12/22 14:33:08 UTC

XMLURL memory allocation problem

The following code:

#include <vector>
#include <xercesc/util/XMLURL.hpp>

using namespace std;
XERCES_CPP_NAMESPACE_USE

int main()
{
  vector<XMLURL> urls(1, XMLURL());
  return 0;
}

fails to compile under Visual C++ 2003 (VC_7_1) using xerces-c 2.6.0. It 
gives the error:

  C2665: 'xercesc_2_6::XMemory::operator new' : none of the 3 overloads can 
convert parameter 2 from type 'void *'

Apparently the same code compiles fine under gcc 3.4.3 and xerces-c 2.6.0. 
The same problem occurs with XMLUri (and presumably any class derived from 
XMemory). If vector is changed to list, then it compiles.

Any suggestions?

Sam


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: XMLURL memory allocation problem

Posted by Sam Lindley <sa...@redsnapper.net>.
I was wrong... This isn't a problem with VC++. It is just following the 
usual overloading rules, and placement new is hidden. The difference between 
VC++ and gcc is that VC++ uses placement new in its implementation of STL 
containers such as vector. This seems like a perfectly reasonable 
implementation decision.

The fix appears to work: add placement new (and delete) to the XMemory 
class. Is there any reason why this hasn't been done already?

Sam

----- Original Message ----- 
From: "Sam Lindley" <sa...@redsnapper.net>
To: <xe...@xml.apache.org>
Sent: Friday, December 24, 2004 1:46 AM
Subject: Re: XMLURL memory allocation problem


>I think I've found the root of the problem, and it seems to be something 
>that's been noticed before, e.g:
>
>  http://marc.theaimsgroup.com/?l=xerces-c-dev&m=107349519306035&w=2
>
> AFAICS, VC++ (2003) doesn't handle overloading of operator new properly. 
> The XMemory class overloads placement new for allocating XMemory objects 
> using a memory manager:
>
>  void* operator new(size_t size, MemoryManager* memMgr);
>
> but the STL uses placement new, where the second argument is a void 
> pointer:
>
>  void* operator new(size_t size, void* p);
>
> I haven't checked, but I believe the correct behaviour (according to the 
> C++ standard) should be that the original placement new (with void 
> pointer) remains visible. This appears to be what happens in the gcc 
> (3.4.3), but not in VC++. It seems that overloading new for a particular 
> class obscures all of the original global overloadings of new with respect 
> to that class (even when not all of the versions of new have been 
> overloaded in the class). The easy workaround is to copy any of the 
> original global definitions which haven't already been overloaded. In the 
> case of the XMemory class, the void* variant of placement new can be 
> overloaded using the original definition:
>
>  void* operator new(size_t size, void* p) {return p;}
>
> Now, I seem to have solved the problem... but was it actually safe to use 
> XMemory objects in STL containers in the first place?
>
> Sam
>
> ----- Original Message ----- 
> From: "Sam Lindley" <sa...@redsnapper.net>
> To: <xe...@xml.apache.org>
> Sent: Wednesday, December 22, 2004 1:33 PM
> Subject: XMLURL memory allocation problem
>
>
>> The following code:
>>
>> #include <vector>
>> #include <xercesc/util/XMLURL.hpp>
>>
>> using namespace std;
>> XERCES_CPP_NAMESPACE_USE
>>
>> int main()
>> {
>>  vector<XMLURL> urls(1, XMLURL());
>>  return 0;
>> }
>>
>> fails to compile under Visual C++ 2003 (VC_7_1) using xerces-c 2.6.0. It 
>> gives the error:
>>
>>  C2665: 'xercesc_2_6::XMemory::operator new' : none of the 3 overloads 
>> can convert parameter 2 from type 'void *'
>>
>> Apparently the same code compiles fine under gcc 3.4.3 and xerces-c 
>> 2.6.0. The same problem occurs with XMLUri (and presumably any class 
>> derived from XMemory). If vector is changed to list, then it compiles.
>>
>> Any suggestions?
>>
>> Sam


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org


Re: XMLURL memory allocation problem

Posted by Sam Lindley <sa...@redsnapper.net>.
I think I've found the root of the problem, and it seems to be something 
that's been noticed before, e.g:

  http://marc.theaimsgroup.com/?l=xerces-c-dev&m=107349519306035&w=2

AFAICS, VC++ (2003) doesn't handle overloading of operator new properly. The 
XMemory class overloads placement new for allocating XMemory objects using a 
memory manager:

  void* operator new(size_t size, MemoryManager* memMgr);

but the STL uses placement new, where the second argument is a void pointer:

  void* operator new(size_t size, void* p);

I haven't checked, but I believe the correct behaviour (according to the C++ 
standard) should be that the original placement new (with void pointer) 
remains visible. This appears to be what happens in the gcc (3.4.3), but not 
in VC++. It seems that overloading new for a particular class obscures all 
of the original global overloadings of new with respect to that class (even 
when not all of the versions of new have been overloaded in the class). The 
easy workaround is to copy any of the original global definitions which 
haven't already been overloaded. In the case of the XMemory class, the void* 
variant of placement new can be overloaded using the original definition:

  void* operator new(size_t size, void* p) {return p;}

Now, I seem to have solved the problem... but was it actually safe to use 
XMemory objects in STL containers in the first place?

Sam

----- Original Message ----- 
From: "Sam Lindley" <sa...@redsnapper.net>
To: <xe...@xml.apache.org>
Sent: Wednesday, December 22, 2004 1:33 PM
Subject: XMLURL memory allocation problem


> The following code:
>
> #include <vector>
> #include <xercesc/util/XMLURL.hpp>
>
> using namespace std;
> XERCES_CPP_NAMESPACE_USE
>
> int main()
> {
>  vector<XMLURL> urls(1, XMLURL());
>  return 0;
> }
>
> fails to compile under Visual C++ 2003 (VC_7_1) using xerces-c 2.6.0. It 
> gives the error:
>
>  C2665: 'xercesc_2_6::XMemory::operator new' : none of the 3 overloads can 
> convert parameter 2 from type 'void *'
>
> Apparently the same code compiles fine under gcc 3.4.3 and xerces-c 2.6.0. 
> The same problem occurs with XMLUri (and presumably any class derived from 
> XMemory). If vector is changed to list, then it compiles.
>
> Any suggestions?
>
> Sam
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
> For additional commands, e-mail: xerces-c-dev-help@xml.apache.org
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: xerces-c-dev-unsubscribe@xml.apache.org
For additional commands, e-mail: xerces-c-dev-help@xml.apache.org