Index: kstars/skycomponents/deepstarcomponent.h =================================================================== --- kstars/skycomponents/deepstarcomponent.h +++ kstars/skycomponents/deepstarcomponent.h @@ -129,7 +129,7 @@ long unsigned t_drawUnnamed { 0 }; long unsigned t_updateCache { 0 }; - QVector m_starBlockList; + QVector> m_starBlockList; QHash m_CatalogNumber; bool staticStars { false }; Index: kstars/skycomponents/deepstarcomponent.cpp =================================================================== --- kstars/skycomponents/deepstarcomponent.cpp +++ kstars/skycomponents/deepstarcomponent.cpp @@ -50,8 +50,6 @@ DeepStarComponent::~DeepStarComponent() { - qDeleteAll(m_starBlockList); - m_starBlockList.clear(); if (fileOpened) starReader.closeFile(); fileOpened = false; @@ -117,9 +115,9 @@ { Trixel trixel = i; quint64 records = starReader.getRecordCount(i); - StarBlock* SB = new StarBlock(records); + std::shared_ptr SB(new StarBlock(records)); - if (!SB) + if (!SB.get()) qDebug() << "ERROR: Could not allocate new StarBlock to hold shallow unnamed stars for trixel " << trixel << endl; @@ -168,9 +166,9 @@ { Trixel trixel = i; quint64 records = starReader.getRecordCount(i); - StarBlock *SB = new StarBlock(records); + std::shared_ptr SB(new StarBlock(records)); - if (!SB) + if (!SB.get()) qDebug() << "ERROR: Could not allocate new StarBlock to hold shallow unnamed stars for trixel " << trixel << endl; @@ -322,8 +320,8 @@ Trixel currentRegion = region.next(); for (int i = 0; i < m_starBlockList.at(currentRegion)->getBlockCount(); ++i) { - StarBlock *prevBlock = ((i >= 1) ? m_starBlockList.at(currentRegion)->block(i - 1) : nullptr); - StarBlock *block = m_starBlockList.at(currentRegion)->block(i); + std::shared_ptr prevBlock = ((i >= 1) ? m_starBlockList.at(currentRegion)->block(i - 1) : std::shared_ptr()); + std::shared_ptr block = m_starBlockList.at(currentRegion)->block(i); if (i == 0 && !m_StarBlockFactory->markFirst(block)) qDebug() << "markFirst failed in trixel" << currentRegion; @@ -348,6 +346,10 @@ // TODO: Is there a better way? We may have to change the magnitude tolerance if the catalog changes // Static stars need not execute fillToMag + // Safety check if the current region is in star block list + if ((int)currentRegion >= m_starBlockList.size()) + continue; + if (!staticStars && !m_starBlockList.at(currentRegion)->fillToMag(maglim) && maglim <= m_FaintMagnitude * (1 - 1.5 / 16)) { @@ -360,7 +362,7 @@ // << m_starBlockList[ currentRegion ]->getBlockCount() << " blocks" << endl; // REMARK: The following should never carry state, except for const parameters like updateID and maglim - std::function mapFunction = [&updateID, &maglim](StarBlock *myBlock) { + std::function)> mapFunction = [&updateID, &maglim](std::shared_ptr myBlock) { for (StarObject &star : myBlock->contents()) { if (star.updateID != updateID) @@ -374,7 +376,7 @@ for (int i = 0; i < m_starBlockList.at(currentRegion)->getBlockCount(); ++i) { - StarBlock *block = m_starBlockList.at(currentRegion)->block(i); + std::shared_ptr block = m_starBlockList.at(currentRegion)->block(i); // qDebug() << "---> Drawing stars from block " << i << " of trixel " << // currentRegion << ". SB has " << block->getStarCount() << " stars" << endl; for (int j = 0; j < block->getStarCount(); j++) @@ -466,8 +468,9 @@ qDebug() << " Sky Mesh Size: " << m_skyMesh->size(); for (long int i = 0; i < m_skyMesh->size(); i++) { - StarBlockList *sbl = new StarBlockList(i, this); - if (!sbl) + std::shared_ptr sbl(new StarBlockList(i, this)); + + if (!sbl.get()) { qDebug() << "nullptr starBlockList. Expect trouble!"; } @@ -508,9 +511,14 @@ while (region.hasNext()) { Trixel currentRegion = region.next(); + + // Safety check if the current region is in star block list + if ((int)currentRegion >= m_starBlockList.size()) + continue; + for (int i = 0; i < m_starBlockList.at(currentRegion)->getBlockCount(); ++i) { - StarBlock *block = m_starBlockList.at(currentRegion)->block(i); + std::shared_ptr block = m_starBlockList.at(currentRegion)->block(i); for (int j = 0; j < block->getStarCount(); ++j) { #ifdef KSTARS_LITE @@ -570,11 +578,11 @@ Trixel currentRegion = region.next(); // FIXME: Build a better way to iterate over all stars. // Ideally, StarBlockList should have such a facility. - StarBlockList *sbl = m_starBlockList[currentRegion]; + std::shared_ptr sbl = m_starBlockList[currentRegion]; sbl->fillToMag(maglim); for (int i = 0; i < sbl->getBlockCount(); ++i) { - StarBlock *block = sbl->block(i); + std::shared_ptr block = sbl->block(i); for (int j = 0; j < block->getStarCount(); ++j) { #ifdef KSTARS_LITE @@ -619,11 +627,13 @@ { float faintMag = -5.0; bool integrity = true; + for (Trixel trixel = 0; trixel < (unsigned int)m_skyMesh->size(); ++trixel) { for (int i = 0; i < m_starBlockList[trixel]->getBlockCount(); ++i) { - StarBlock *block = m_starBlockList[trixel]->block(i); + std::shared_ptr block = m_starBlockList[trixel]->block(i); + if (i == 0) faintMag = block->getBrightMag(); // NOTE: Assumes 2 decimal places in magnitude field. TODO: Change if it ever does change @@ -635,7 +645,7 @@ } if (i > 1 && (!block->prev)) qDebug() << "Trixel " << trixel << ": ERROR: Block" << i << "is unlinked in LRU Cache"; - if (block->prev && block->prev->parent == m_starBlockList[trixel] && + if (block->prev && block->prev->parent == m_starBlockList[trixel].get() && block->prev != m_starBlockList[trixel]->block(i - 1)) { qDebug() << "Trixel " << trixel Index: kstars/skycomponents/starblock.h =================================================================== --- kstars/skycomponents/starblock.h +++ kstars/skycomponents/starblock.h @@ -146,8 +146,8 @@ float faintMag; float brightMag; StarBlockList *parent; - StarBlock *prev; - StarBlock *next; + std::shared_ptr prev; + std::shared_ptr next; quint32 drawID; private: Index: kstars/skycomponents/starblock.cpp =================================================================== --- kstars/skycomponents/starblock.cpp +++ kstars/skycomponents/starblock.cpp @@ -64,9 +64,8 @@ StarBlock::~StarBlock() { - if (parent) - parent->releaseBlock(this); } + #ifdef KSTARS_LITE StarNode *StarBlock::addStar(const starData &data) { Index: kstars/skycomponents/starblockfactory.h =================================================================== --- kstars/skycomponents/starblockfactory.h +++ kstars/skycomponents/starblockfactory.h @@ -51,14 +51,14 @@ * * @return A StarBlock that is available for use */ - StarBlock *getBlock(); + std::shared_ptr getBlock(); /** * @short Mark a StarBlock as most recently used and sync its drawID with the current drawID * * @return true on success, false if the StarBlock supplied was not on our list at all */ - bool markFirst(StarBlock *block); + bool markFirst(std::shared_ptr& block); /** * @short Rank a given StarBlock after another given StarBlock in the LRU list @@ -68,7 +68,7 @@ * @param block The block to mark for use * @return true on success, false on failure */ - bool markNext(StarBlock *after, StarBlock *block); + bool markNext(std::shared_ptr& after, std::shared_ptr& block); /** * @short Returns the number of StarBlocks currently produced @@ -111,7 +111,7 @@ */ int deleteBlocks(int nblocks); - StarBlock *first, *last; // Pointers to the beginning and end of the linked list + std::shared_ptr first, last; // Pointers to the beginning and end of the linked list int nBlocks; // Number of blocks we currently have in the cache int nCache; // Number of blocks to start recycling cached blocks at Index: kstars/skycomponents/starblockfactory.cpp =================================================================== --- kstars/skycomponents/starblockfactory.cpp +++ kstars/skycomponents/starblockfactory.cpp @@ -50,14 +50,14 @@ pInstance = nullptr; } -StarBlock *StarBlockFactory::getBlock() +std::shared_ptr StarBlockFactory::getBlock() { - StarBlock *freeBlock = nullptr; + std::shared_ptr freeBlock; if (nBlocks < nCache) { - freeBlock = new StarBlock; - if (freeBlock) + freeBlock.reset(new StarBlock); + if (freeBlock.get()) { ++nBlocks; return freeBlock; @@ -83,15 +83,16 @@ freeBlock->next = nullptr; return freeBlock; } - freeBlock = new StarBlock; - if (freeBlock) + freeBlock.reset(new StarBlock); + if (freeBlock.get()) ++nBlocks; + return freeBlock; } -bool StarBlockFactory::markFirst(StarBlock *block) +bool StarBlockFactory::markFirst(std::shared_ptr& block) { - if (!block) + if (!block.get()) return false; // fprintf(stderr, "markFirst()!\n"); @@ -129,16 +130,16 @@ return true; } -bool StarBlockFactory::markNext(StarBlock *after, StarBlock *block) +bool StarBlockFactory::markNext(std::shared_ptr& after, std::shared_ptr& block) { // fprintf(stderr, "markNext()!\n"); - if (!block || !after) + if (!block.get() || !after.get()) { qDebug() << "WARNING: markNext called with nullptr argument" << endl; return false; } - if (!first) + if (!first.get()) { qDebug() << "WARNING: markNext called without an existing linked list" << endl; return false; @@ -244,12 +245,12 @@ int StarBlockFactory::deleteBlocks(int nblocks) { int i = 0; - StarBlock *temp = nullptr; + std::shared_ptr temp; while (last != nullptr && i != nblocks) { temp = last->prev; - delete last; + last.reset(); last = temp; i++; } @@ -266,7 +267,7 @@ void StarBlockFactory::printStructure() const { - StarBlock *cur = nullptr; + std::shared_ptr cur; Trixel curTrixel = 513; // TODO: Change if we change HTMesh level int index = 0; bool draw = false; @@ -297,12 +298,12 @@ int StarBlockFactory::freeUnused() { int i = 0; - StarBlock *temp = nullptr; + std::shared_ptr temp; while (last != nullptr && last->drawID < drawID && i != nBlocks) { temp = last->prev; - delete last; + last.reset(); last = temp; i++; } Index: kstars/skycomponents/starblocklist.h =================================================================== --- kstars/skycomponents/starblocklist.h +++ kstars/skycomponents/starblocklist.h @@ -67,7 +67,7 @@ * * @param Pointer to the StarBlock */ - void setStaticBlock(StarBlock *block); + void setStaticBlock(std::shared_ptr &block); /** * @short Drops the StarBlock with the given pointer from the list @@ -82,12 +82,12 @@ * @param Index of the required block * @return The StarBlock requested for, nullptr if index out of bounds */ - inline StarBlock *block(unsigned int i) { return ((i < nBlocks) ? blocks[i] : nullptr); } + inline std::shared_ptr block(unsigned int i) { return ((i < nBlocks) ? blocks[i] : std::shared_ptr()); } /** * @return a const reference to the contents of this StarBlockList */ - inline const QList &contents() const { return blocks; } + inline const QList> &contents() const { return blocks; } /** * @short Returns the total number of stars in this StarBlockList @@ -118,7 +118,7 @@ unsigned long nStars; long readOffset; float faintMag; - QList blocks; + QList> blocks; unsigned int nBlocks; bool staticStars; DeepStarComponent *parent; Index: kstars/skycomponents/starblocklist.cpp =================================================================== --- kstars/skycomponents/starblocklist.cpp +++ kstars/skycomponents/starblocklist.cpp @@ -42,14 +42,11 @@ StarBlockList::~StarBlockList() { - // NOTE: Rest of the StarBlocks are taken care of by StarBlockFactory - if (staticStars && blocks[0]) - delete blocks[0]; } int StarBlockList::releaseBlock(StarBlock *block) { - if (block != blocks[nBlocks - 1]) + if (block != blocks[nBlocks - 1].get()) qDebug() << "ERROR: Trying to release a block which is not the last block! Trixel = " << trixel << endl; else if (blocks.size() > 0) @@ -128,9 +125,9 @@ if (nBlocks == 0 || blocks[nBlocks - 1]->isFull()) { - StarBlock *newBlock; - newBlock = SBFactory->getBlock(); - if (!newBlock) + std::shared_ptr newBlock = SBFactory->getBlock(); + + if (!newBlock.get()) { qWarning() << "ERROR: Could not get a new block from StarBlockFactory::getBlock() in trixel " << trixel << ", while trying to create block #" << nBlocks + 1 << endl; @@ -176,7 +173,7 @@ return ((maglim < faintMag) ? true : false); } -void StarBlockList::setStaticBlock(StarBlock *block) +void StarBlockList::setStaticBlock(std::shared_ptr &block) { if (!block) return; Index: tools/valgrind.supp =================================================================== --- tools/valgrind.supp +++ tools/valgrind.supp @@ -318,6 +318,12 @@ ... } { + Qt21 + Memcheck:Cond + fun:_ZN12QtConcurrent18BlockSizeManagerV214timeBeforeUserEv + ... +} +{ KDE1 Memcheck:Leak ...