Getting an object to exist out of scope

Aug 5, 2010 at 6:18pm
I have a program which roughly works like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//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.
Last edited on Aug 5, 2010 at 6:22pm
Aug 5, 2010 at 6:26pm
Chemical Engineer wrote:
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.

They do? Maybe something else is wrong...
Last edited on Aug 5, 2010 at 6:40pm
Aug 5, 2010 at 6:44pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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?
Last edited on Aug 5, 2010 at 6:45pm
Aug 5, 2010 at 6:52pm
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.
Aug 5, 2010 at 7:03pm
This works ok for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
using namespace 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?...
Last edited on Aug 5, 2010 at 7:06pm
Aug 5, 2010 at 7:22pm
It's
1
2
3
4
5
6
7
8
9
10
11
Points *OpenPointsFile (char fileName[150]){
    double xValue, yValue;
    ifstream in(fileName);
    Points *tempPoints = new Points;
    while(in >> xValue){
        in >> yValue;
        tempPoints->addPoint(xValue, yValue);
    }
    in.close();
    return tempPoints;
}


The class Points is a linked list of a struct Point with some regression features.
Last edited on Aug 5, 2010 at 7:26pm
Aug 5, 2010 at 8:00pm
The problem is that the '\' character is the escape character in C++, so "\\" is interpreted as a single '\'.

Just use '/' instead.

Oh...and it's int main()
Last edited on Aug 5, 2010 at 8:00pm
Aug 5, 2010 at 8:10pm
I noticed that m4ster r0shi used int main(), too. Why would this be preferable to void main()?
Aug 5, 2010 at 8:19pm
Because void main() is not allowed by the standard, although some compilers accept it as an extension.
Last edited on Aug 5, 2010 at 8:20pm
Aug 5, 2010 at 8:31pm
it's also const char [] for string literals and not char []
Topic archived. No new replies allowed.