0

The following code instead of returning a pointer back to an audioResource it returns something else which is invalid, i've gone through with a debugger and the problem is with this line

return *list_it;

Here is my function:

AudioResource* AudioManager::getResource(const unsigned int ID)
{
    std::list<AudioResource*>::iterator list_it;
    for(list_it = m_resources.begin(); list_it!= m_resources.end(); list_it++)
    {
        if((*list_it)->getID()==ID)
            {
std::cout<<*(*list_it)->getFileName();
            return *list_it;
        }
    }
    return nullptr;
}

O and I have tried putting it as (*list_it) but i got the same results =s

How it is populated...

Resource* AudioManager::addResource(const unsigned int ID, 
      const std::string fileName,  const unsigned int scope,
      const std::string type)
{
     AudioResource* temp;
     if(type == "AUDIO_TYPE_SAMPLE")
     {
          temp = new AudioResource(ID,fileName,scope,
                      RESOURCE_AUDIO,AUDIO_TYPE_SAMPLE);
          m_resources.push_back(temp);
     }
     else if(type == "AUDIO_TYPE_STREAM")
     {
          temp = new AudioResource(ID,fileName,scope,
                    RESOURCE_AUDIO,AUDIO_TYPE_STREAM);
          m_resources.push_back(temp);
     }

     return temp;
}

call to get resource

cout<<AudioManager::getInstance()->getResource(IDnum)->getFileName();
21
  • Design thoughts: Why a list not a vector? Or a map? Why raw pointers? :) Commented Mar 1, 2012 at 14:19
  • 3
    are you sure that AudioResource* is valid? Maybe the iterator is correct, is the backing list that's wrong. Commented Mar 1, 2012 at 14:20
  • 1
    Looks okay as is (nit: post-increment might give worse performance). How do you initialize m_resources? Commented Mar 1, 2012 at 14:20
  • 2
    Can you show how m_resources is populated? Commented Mar 1, 2012 at 14:20
  • Everything looks ok. I'd say some other code deleted a pointer from the collection and didn't remove it or set it to nullptr. Commented Mar 1, 2012 at 14:22

2 Answers 2

2

If type is neither of the two values an uninitialized pointer is added to m_resources:

AudioResource* temp;
if(type == "AUDIO_TYPE_SAMPLE")
{
    temp = new AudioResource(ID,fileName,scope,RESOURCE_AUDIO,AUDIO_TYPE_SAMPLE);
}
else if(type == "AUDIO_TYPE_STREAM")
{
    temp = new AudioResource(ID,fileName,scope,RESOURCE_AUDIO,AUDIO_TYPE_STREAM);
}
m_resources.push_back(temp);

Initialize temp to NULL and only add to m_resources if temp != NULL.

Also, the function returns the same uninitialized pointer.

Sign up to request clarification or add additional context in comments.

9 Comments

Code has been amended so that nothing is pushed onto the list if is not in the else statement and problem still is there, thanks for trying tho =)
The uninitialized pointer is still being returned: it is used by the caller?
No for now the return data is ignored, as I have not finished coding the rest of the resource manager =)
But that isn't his code. He is doing push_back within the ifs. True the function will return an invalid pointer but it doesn't get added to the list as far as I see.
@ChrisCondy, do elements get removed from m_resources anywhere?
|
1

You return nullptr in case the ID doesn't exist, but you never check against it at the call site, which will give you a null pointer access if the ID doesn't exist and which will likely create problems.

AudioManager::getInstance()->getResource(IDnum)->getFileName();

Change that to

AudioResource* res = AudioManager::getInstance()->getResource(IDnum);
if(res)
  std::cout << res->getFileName();

11 Comments

Code has been amended so this problem is avoided and still the orignal problem is there
where does he push it into the list?
@ChrisCondy: Do you have any code that removes things from the list? Do you have concurrent accesses to the list?
@CashCow: If you would've read the other comments, you would have noticed that the OP changed the code after our answers pointed the flaws out.
Nothing removes from the list and no i am not multithreading =)
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.