Problem with AcArray

Problem with AcArray

Kyudos
Advisor Advisor
1,324 Views
2 Replies
Message 1 of 3

Problem with AcArray

Kyudos
Advisor
Advisor

At the end of one of my functions, my code is throwing a heap exception when doing a 'RemoveAll' on an AcArray<int>.

The array is declared at the start of the function.

 

Can I assume this is not really a problem with AcArray? Something else is causing it?

0 Likes
Accepted solutions (1)
1,325 Views
2 Replies
Replies (2)
Message 2 of 3

Kyudos
Advisor
Advisor
Accepted solution

Yeah - it was caused elsewhere!

0 Likes
Message 3 of 3

Anonymous
Not applicable

As far as i know, AcArray uses acdbAlloc/acdbFree for memory management.

If you want to use default malloc/free for your library.

 

You can explicitly allocate physical buffer for AcArray types to avoid this

e.g.)

  AcArray<AcGePoint3d> output;

  output.setPhysicalLength(<enough_size>);

  entity->some_function(output);

 

If you don't know how much buffer to be required, then you should explicitly deallocate with it

Here is some of my template code for deallocation


// Explicit AcArray memory buffer deallocation
template<typename T>
void MyAcArrayFree(AcArray<T>& buffer)
{
    // Only if allocated with ::acdbAlloc
    if (0 == buffer.physicalLength())
    return;

    // AcArray transparent structure
    struct AcArrayView
    {
        T* mpArray;
        int mPhysicalLen; // Actual buffer length.
        int mLogicalLen; // Number of items in the array.
        int mGrowLen; // Buffer grows by this value.
    };
    static_assert(sizeof(AcArray<T>) == sizeof(AcArrayView));

    // Force cast to AcArray transparent structure
    auto pArrayView = reinterpret_cast<AcArrayView*>(std::addressof(buffer));

    // Call dtor data type T has destructor
    if constexpr (std::is_destructible_v<T>)
    {
        for (size_t i = 0; i < pArrayView->mLogicalLen; ++i)
        {
            (pArrayView->mpArray + i)->~T();
        }
    }

    // Explicitly deallocate buffer
    ::acdbFree(pArrayView->mpArray);

    // Reset to Zero
    pArrayView->mpArray = nullptr;
    pArrayView->mPhysicalLen = 0;
    pArrayView->mLogicalLen = 0;
}