Use a reference to MyObject in your for loop:
for (MyObject& object : _objects)
That avoids the problem with the
object
variable going out of scope.
But allow me the remark that it is usually not a good practice to throw an exception for a "not found" condition. This makes your code more difficult to use and causes unnecessary headaches for a rather common situation. Your approach returning a pointer and NULL in the case that the object has not been found is probably better. And with the above correction of the for loop it will work.