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 Konstantin Kivi <ko...@sirena2000.ru> on 2001/04/11 17:31:52 UTC

memory management

 From the xerces docs:
>Key points to remember when using the C++ DOM classes:
>   Create them as local variables, or as member 
>   variables of some other class.   Never "new" a 
>   DOM object into the heap or make an ordinary C 
>   pointer variable to one, as this will greatly 
>   confuse the automatic memory management. 

Why can't I 'new' dom objects? Is this wrong practise
unconditionally, or I only must be very careful?

what is the difference between

{
        DOM_Document *pdoc=new 
                DOM_Document(impl.createDocument(...));
                
...........................
        delete pdoc;
}

and
{
        DOM_Document doc=impl.createDoc(...);
......................
        
}

why I can make those objects being parts of the 
stucture and then alloc the that structure?
(the doc doesn't say I can't do so)

What I exactly need is to pass the pointer 
to DOM_Document or its equivalent (call it handle)
between C and C++ parts of the programm.

Sorry if I am asking something really stupid

-- 
Sincerely Yours, Konstantin Kivi, Russia, konst@sirena2000.ru


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


Re: memory management

Posted by Miroslaw Dobrzanski-Neumann <mn...@mosaic-ag.com>.
On Wed, Apr 11, 2001 at 04:26:09PM -0400, Cem Karan wrote:
> Anyone who wants to jump in and correct me, please do so.
> 
> Xerces tries to use reference counting wherever possible to
> automatically dispose of unused memory in much the same way that Java
> does it.  If you actually use new and delete, then the built-in garbage
> collection won't work because the reference counting will be wrong.

In my application I used xerces 1.3 to parse a 39 MB XML message and sotre it
in a DOM. I used "libefence" both on Linux and AIX 4.3.2 to check for memory
errors. If I protect freed memory blocks from beeing reused again I get out of
memory.  My hard limits were:
- Linux 1.0 GB allocatable memory (RAM and swap)
- AIX 1.5 GB hard coded with -bmaxdata when linking (there was enough RAM 2GB)

=> There are a very lot of calls to malloc/new. Since malloc/new are generally
no very cheap.

regards
-- 
Miroslaw Dobrzanski-Neumann

MOSAIC SOFTWARE AG
Base Development and Research
Tel +49-2225-882-291
Fax +49-2225-882-201
E-mail: mne@mosaic-ag.com


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


Re: memory management

Posted by Konstantin Kivi <ko...@sirena2000.ru>.
On Wed, Apr 11, 2001 at 04:26:09PM -0400, Cem Karan wrote:
> Anyone who wants to jump in and correct me, please do so.
> 
> Xerces tries to use reference counting wherever possible to
> automatically dispose of unused memory in much the same way that Java
> does it.  If you actually use new and delete, then the built-in garbage
> collection won't work because the reference counting will be wrong.

I understand about reference counting, but I cannot get how
new and delete in the example below will fool memory
management. Or xerces use garbage collection that looks
at allocated memory regions and try to figure out if
it is time to free the region. It is not reference counting.

> > 
> > what is the difference between
> > 
> > {
> >         DOM_Document *pdoc=new
> >                 DOM_Document(impl.createDocument(...));
> > 
> > ...........................
> >         delete pdoc;
> > }
> > 
> > and
> > {
> >         DOM_Document doc=impl.createDoc(...);
> > ......................
> > 
> > }
> > 

As I stated earliar I need to be able to pass pointer to
doc out of C++ function to C part of the programm
and the have it passed back like a handle.


-- 
Sincerely Yours, Konstantin Kivi, Russia, konst@sirena2000.ru


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


Re: memory management

Posted by Cem Karan <Ce...@usa.alcatel.com>.
Anyone who wants to jump in and correct me, please do so.

Xerces tries to use reference counting wherever possible to
automatically dispose of unused memory in much the same way that Java
does it.  If you actually use new and delete, then the built-in garbage
collection won't work because the reference counting will be wrong.

As a side note, has anyone considered overriding new and delete to
permanently solve this problem?

Cem

Konstantin Kivi wrote:
> 
>  From the xerces docs:
> >Key points to remember when using the C++ DOM classes:
> >   Create them as local variables, or as member
> >   variables of some other class.   Never "new" a
> >   DOM object into the heap or make an ordinary C
> >   pointer variable to one, as this will greatly
> >   confuse the automatic memory management.
> 
> Why can't I 'new' dom objects? Is this wrong practise
> unconditionally, or I only must be very careful?
> 
> what is the difference between
> 
> {
>         DOM_Document *pdoc=new
>                 DOM_Document(impl.createDocument(...));
> 
> ...........................
>         delete pdoc;
> }
> 
> and
> {
>         DOM_Document doc=impl.createDoc(...);
> ......................
> 
> }
> 
> why I can make those objects being parts of the
> stucture and then alloc the that structure?
> (the doc doesn't say I can't do so)
> 
> What I exactly need is to pass the pointer
> to DOM_Document or its equivalent (call it handle)
> between C and C++ parts of the programm.
> 
> Sorry if I am asking something really stupid
> 
> --
> Sincerely Yours, Konstantin Kivi, Russia, konst@sirena2000.ru

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


Re: memory management

Posted by Aaron Kaplan <ka...@xsoft.at>.
On Wed, 11 Apr 2001, Konstantin Kivi wrote:

>  From the xerces docs:
> >Key points to remember when using the C++ DOM classes:
> >   Create them as local variables, or as member 
> >   variables of some other class.   Never "new" a 
> >   DOM object into the heap or make an ordinary C 
> >   pointer variable to one, as this will greatly 
> >   confuse the automatic memory management. 
> 
> Why can't I 'new' dom objects? Is this wrong practise
> unconditionally, or I only must be very careful?
> 
> what is the difference between
> 
> {
>         DOM_Document *pdoc=new 
>                 DOM_Document(impl.createDocument(...));
>                 
> ...........................
>         delete pdoc;
> }
> 
> and
> {
>         DOM_Document doc=impl.createDoc(...);
> ......................
>         
> }
> 
> why I can make those objects being parts of the 
> stucture and then alloc the that structure?
> (the doc doesn't say I can't do so)
> 
> What I exactly need is to pass the pointer 
> to DOM_Document or its equivalent (call it handle)
> between C and C++ parts of the programm.

No, it is not a stupid question.... I had to fight with the same problem
for 2 weeks until it worked. Mem mgmt on xerces is probably a good idea 
by itself but as soon as you want to interface with other langs it turns
out to be a terrible thing which is always in the way.


Ok, here is how I was sucessfull:
write a (C++) wrapper class. The class has an array 
(lets call it my_dom_node_refs_workaround_array, or simply "arr" for
short):

  class wrapper {
  private:
    Dom_Node arr[MAX_NODES];
    unsigned int last_arr_idx;    // last used array index
 }

 when you want to keep a DOM_Node somewhere simply go like this:

  arr[last_arr_idx++] = my_dom_node;


so whenever you want to pass a pointer/reference to the calling C program
you would return the _array_index and not the address of the DOM_Node.

When your calling C program is finished, let it call a cleanup method:

  class wrapper {

  public:
   BOOL cleanup() {
    for (int i = 0; i < MAX_NODES; i++) {
      arr[i] = 0;   // by this the node references will be decremented 
                    // and eventually all DOM_Node mem will be freed
    } // end for
  } // end func
 } // end class



> 



hope it helped,

aaron.



> Sorry if I am asking something really stupid
> 
> -- 
> Sincerely Yours, Konstantin Kivi, Russia, konst@sirena2000.ru
> 
> 
> ---------------------------------------------------------------------
> 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