views:

92

answers:

2

So I am coding this client/server program. This code is from the client side. The client has an instance of an object

mpqs_sieve *instance_;

The reason I make it as a pointer is, that mpqs_sieve only has a constructor that takes 3 arguments, and I want to instantiate it at a later point in time.

The client first gets some data from the server, and uses this to instantiate instance_. After this, it will request some more data, and upon receiving this (these are three coefficients for a quadratic polynomial), it should set these in the instance_ object. However upon calling a member function of instance_, I get an access violation on one of the members of instance_ within that function call.

I posted my code here: on pastebin, and I get the error on line 100. The call comes from line 71, and before that line 21. Here's an excerpt:

class client_protocol {
public:
    static std::string parse_message(
        network_message& msg, mpqs_sieve *instance_)
    {
        // ...
        return set_mpqs_data(m.substr(i+1), instance_);
    }

private:
    static std::string set_mpqs_data(
        std::string data, mpqs_sieve *instance_)
    {
        instance_ = new mpqs_sieve(n, M, FB_count);
        // ...
    }
};

Any ideas to solve this?

+3  A: 

You are passing a copy of the instance_ pointer to the function, not a reference to the variable. When you assign to instance_, you're modifying a local variable, not the member variable with the same name.

Change the function parameter to mpqs_sieve *&instance_.

avakar
Thanks, this was useful.
Martin Lauridsen
A: 

The note about references to variables in the existing answer is a good point, and I see one other potential issue:

If I understand your code correctly, the object gets created in the set_mpqs_data() function on line 48. Are you sure the set_mpqs_data() function is getting called before that time (the MPQS_DATA message is being handled)? Otherwise instance_ might not point at a real object when you call set_polynomial_data().

bde
Hi. Yes I had considered this =)
Martin Lauridsen