Overview
Features
Download
Documentation
Community
Add-Ons & Services

Memory management (Var, Object, Dynamic)

Please post support and help requests here.

Memory management (Var, Object, Dynamic)

Postby devdavid » 27 Mar 2013, 12:04

I am using poco in an iphone project and have som question about memory management. I do get some reported leaks in instruments, See the examples:

Code: Select all
        Var locationRaw = object->get(STORE_LOCATION);
        Object::Ptr locationObject = locationRaw.extract<Object::Ptr>();
        this->storeLocation = new LocationComplexType(locationObject);


Do i need to free allocated memory and how?

The error i get is :

Code: Select all
Leaked Object   #   Address   Size   Responsible Library   Responsible Frame
Malloc 32 Bytes   17   < multiple >   544 Bytes   PocoTest2   StoreRefComplexType::deserialize(Poco::SharedPtr<Poco::JSON::Object, Poco::ReferenceCounter, Poco::ReleasePolicy<Poco::JSON::Object> >)


I also use:

Code: Select all
    Parser parser;
    DefaultHandler handler;
    Array::Ptr rawArr = storeRef.extract<Array::Ptr>();


Do i need to use "delete" or free or something on these?
devdavid
 
Posts: 17
Joined: 11 Mar 2013, 16:10

Re: Memory management (Var, Object, Dynamic)

Postby alex » 28 Mar 2013, 14:47

devdavid wrote:
Code: Select all
        this->storeLocation = new LocationComplexType(locationObject);

Do i need to free allocated memory and how?

depends what this->storeLocation is. If it is a naked pointer then, obviously yes.

devdavid wrote:
Code: Select all
    Parser parser;
    DefaultHandler handler;
    Array::Ptr rawArr = storeRef.extract<Array::Ptr>();


Do i need to use "delete" or free or something on these?

no
alex
 
Posts: 1047
Joined: 11 Jul 2006, 16:27
Location: United_States

Re: Memory management (Var, Object, Dynamic)

Postby devdavid » 29 Apr 2013, 13:54

Code: Select all
this->storeLocation
is a class member of the current class:
Code: Select all
 
class MyType
{
private:
    LocationComplexType *storeLocation;
}

Am I better of using one of POCO's managed types? http://pocoproject.org/slides/030-MemoryManagement.pdf
devdavid
 
Posts: 17
Joined: 11 Mar 2013, 16:10

Re: Memory management (Var, Object, Dynamic)

Postby alex » 29 Apr 2013, 14:13

devdavid wrote:
Code: Select all
this->storeLocation
is a class member of the current class:

Since it is a bare pointer, it is your responsibility to clean it up by explicitly calling delete - best done in the destructor.
devdavid wrote:Am I better of using one of POCO's managed types?

Probably. Be cautious, though - it is either/or - once you assign your pointer to a smart pointer, do not mix it with bare pointers anymore. With AutoPtr, there are mechanisms to do that safely but it is very easy to forget calling duplicate() and end up with dangling pointer, deleting twice etc. If you are sharing the pointer, use SharedPtr. Otherwise, use AutoPtr (your class must inherit from RefCountedObject for this to work). In both cases, it will be deleted automatically.
alex
 
Posts: 1047
Joined: 11 Jul 2006, 16:27
Location: United_States


Return to Support

Who is online

Users browsing this forum: No registered users and 2 guests