How to save and read custom object inherits entity to database ?

Hi, I would like get to know what is the correct way to save custom object to AcDbDatabase in C++.
My problem is when I add about 500 objects, sometimes 600 or more it throws me an exception, while I try
to read earlier added object to database.

This is what I do step by step:

Creates customObject (it inherits Entity), and returns pointer to C#.
```
extern "C" __declspec(dllexport) void* CallCreateCustomObject(bool editable) {
CustomObject* custom = new CustomObject(editable);
return custom;
}
```

Adds customObject to database and returns its ID to C#.
```
extern "C" __declspec(dllexport) LONG_PTR CallAddCustomObjectToDb(AcDbObject* rawCustomObjPtr) {
CustomObject* custom = dynamic_cast(rawCustomObjPtr);
if (!custom) {
acutPrintf(_T("\nERROR: custom object is nullptr"));
return -1;
}

// open db, block table
Acad::ErrorStatus ret = Acad::ErrorStatus::eOk;
AcDbDatabase* pDb = acdbHostApplicationServices()->workingDatabase();

AcDbBlockTable* pBlockTable = NULL;
ret = pDb->getSymbolTable(pBlockTable, AcDb::kForRead);
if (ret != Acad::eOk) {
acutPrintf(_T("\nERROR: getSymbolTable() returned %d"), ret);
}

// add custom to db
AcDbBlockTableRecord* pBlockTableRecord = NULL;
ret = pBlockTable->getAt(ACDB_MODEL_SPACE, pBlockTableRecord, AcDb::kForWrite);
ret = pBlockTable->close();
AcDbObjectId customEntId = AcDbObjectId::kNull;
ret = pBlockTableRecord->appendAcDbEntity(customEntId, custom);
if (ret != Acad::eOk) {
acutPrintf(_T("\nERROR: appendAcDbEntity() returned %d"), ret);
}
pBlockTableRecord->close();

if (customEntId.asOldId() == NULL) {
acutPrintf(_T("\nError custom ID is null"));
}
return customEntId.asOldId();
}

```

subWorldDraw method executing, and problem is when it tries execute acdbOpenAcDbEntity, sometimes it throws
access violation "(TD_DbCore_20.6_15.dll) w bricscad.exe: 0xC0000005: Access violation while reading in localization 0xFFFFFFFF95741CC8."
```
Adesk::Boolean CustomObject::subWorldDraw(AcGiWorldDraw* mode) {
assertReadEnabled();
for (const auto& objectId : subObjects_) {
AcDbEntity* pEntity = nullptr;
Acad::ErrorStatus es = acdbOpenAcDbEntity(pEntity, objectId, AcDb::kForWrite);
if (es == Acad::eOk && pEntity != nullptr) {
pEntity->worldDraw(mode);
}
}