pseudorandom21
Practically a Posting Shark
890 posts since Jan 2011
Reputation Points: 216
Solved Threads: 111
This is a really weird error, and I'm sure there is something you are not telling us. My guess would be that the audioBuffer gets relocated.
The fact that you reallocate or change the std::vector member (I assume is held by value in audioBuffer, as it should) should not cause your pointer to it to be wrong. That's simply impossible in C++. The only way that is possible is if the audioBuffer object gets relocated, which would relocate the std::vector member along with it.
You will need to post more details about the implementation of ResourceHolder and AudioBuffer, because the error cannot be diagnosed from this simple description. One thing for sure, it is not a problem about the std::vector object somehow relocating itself, objects cannot do that in C++, because of value-semantics, objects are only relocated (moved or copied) when their owner (parent object) gets moved.
Generally, it is bad practice to "export" a pointer to a data member from an object. That's dangerous code because you can never be sure that the data member still exist or is still at the same place in memory. Find an alternative. Also, rely on you compiler to optimize things, for example, there is no advantage to do this:
AudioBuffer* audioBuffer = resourceHolder->getAudioBufferPointer();
std::vector<float>* vector = audioBuffer->getVectorPointer();
// blah blah, read the vector
x = vector->at(0);
// use the vector again and again.
Compared to doing this:
// blah blah, read the vector
x = resourceHolder.getAudioBufferPointer().getVectorPointer().at(0);
// use the vector again and again.
Assuming that you return things by reference.
Don't abuse pointers, especially raw pointers (as opposed to smart pointers). They are dangerous because they are ambiguous about ownership. Prefer to hold by value, and if you need to provide access to data members, do so by reference.
mike_2000_17
Posting Virtuoso
2,139 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
>>Could a resizing of the std::vector cause the whole AudioBuffer object to be relocated?
No. Absolutely not. You can eliminate that possibility, and look for another cause. Post more code on the context of the problem, and we might help.
>>If you have any good resource on program design that show / recommend using references over pointers, I'd be very intrested to hear where I can get it!
Well you can look at this FAQ . But, other than that, I don't know.
I've actually been preparing a tutorial on a subject very closely related to this ("avoiding memory problems by design"), as a consequence of doing a code-review on a library with many such problems. Hopefully it will be able to complete it, and post it on this forum, sometime soon.
mike_2000_17
Posting Virtuoso
2,139 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457
> its only when the vector get reallocated in memory (due to another thread loading a different sample into it)
The memory buffer held by the vector could get reallocated.
You need to synchronize access to the vector across multiple threads.
vijayan121
Posting Virtuoso
1,606 posts since Dec 2006
Reputation Points: 1,159
Solved Threads: 287
>>As you've mentioned "avoiding memory problems by design", perhaps I could do a proof read of the tutorial if that suits you?
I finished the tutorial now, and you can read it here if still interested.
mike_2000_17
Posting Virtuoso
2,139 posts since Jul 2010
Reputation Points: 1,634
Solved Threads: 457