//an object class
class MyObject{
}
//function makes the object
MyObject *SomeFunction(char fileName[100]){
MyObject *returnObject = new MyObject;
return returnObject;
}
//code uses the object made by the function
void main(){
MyObject *theObject = SomeFunction("C:\\file.txt");
//object no longer exists here
return;
}
The problem is that, once SomeFunction completes, the MyObject generated by it is destroyed. Attempts to use its pointer result in pointer violations.
How can I get the MyObject generated by SomeFunction to persist outside of SomeFunction?
PS- I know that I can make the MyObject outside of the function and then pass in a reference, but I'd prefer to avoid doing it that way.
The problem is that, once SomeFunction completes, the MyObject generated by it is destroyed.
Your MyObject is not destroyed. What is destroyed is the returnObject pointer. But this happens after being copied to your theObject pointer. So, returnObject is destroyed but now theObject points to your MyObject, which is not destroyed. Everything is ok. Just remeber to delete your MyObject when you don't need it anymore.
Chemical Engineer wrote:
Attempts to use its pointer result in pointer violations.
class MyObject{
}
//function makes the object
MyObject *SomeFunction(char fileName[100]){
MyObject *returnObject = new MyObject;
return returnObject;
}
//code uses the object made by the function
void main(){
MyObject *theObject = SomeFunction("C:\\file.txt");
delete theObject; //error here
return;
}
In the code above, when I comment out the delete theObject;, it doesn't give an error. But any attempt to use the theObject pointer results in an error, including the delete command.
After I tracked down the error, it made sense to me that the object would be deleted by going outside of the scope in which it was created (the function). That seems to be the case, but is that not how it works?
I'm not sure it turned out like this, but it turns out the file name had a single backslash instead of two in the actual filename that I was using. Fixing this fixed the problem.
#include <iostream>
usingnamespace std;
class MyObject
{
public:
MyObject()
{
cout << "constructor of MyObject at " << this << endl;
}
~MyObject()
{
cout << "destructor of MyObject at " << this << endl;
}
};
MyObject * SomeFunction()
{
cout << "-=-=-=-=inside SomeFunction=-=-=-=-\n" << endl;
MyObject * returnObject = new MyObject;
cout << "address of returnObject: " << &returnObject << endl;
cout << "address of MyObject: " << returnObject << endl;
cout << "\n-=-=-=-=exiting SomeFunction=-=-=-=-\n" << endl;
return returnObject;
}
int main()
{
MyObject * theObject = SomeFunction();
cout << "address of theObject: " << &theObject << endl;
cout << "address of MyObject: " << theObject << endl;
delete theObject;
cout << "\nhit enter to quit";
cin.get();
return 0;
}
Chemical Engineer wrote:
I'm not sure it turned out like this, but it turns out the file name had a single backslash instead of two in the actual filename that I was using. Fixing this fixed the problem.
Hmmm... At least you now know that it wasn't a pointer problem :P Would you mind posting the full version of your SomeFunction function?...