CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Feb 2009
    Posts
    4

    Question Array of Object Pointers

    I am having trouble getting Access Violation errors when I try to manipulate an array of object pointers.

    I am trying to maintain an array of object pointers as a member of a class. I want to access the objects in the array in many different member methods in the class. I have a method specifically for initializing the elements in the array as necessary:

    void ApplicationQueueHandler::createLocalQueue(qc::LocalQueue* lq, std::string queueName)
    {
    qc::SubscriptionManager subs(session);
    subs.subscribe(*lq, queueName);
    }

    This method is used:


    localAppQueues[localQueueCount] = new qc::LocalQueue;
    qc::LocalQueue* lq = localAppQueues[localQueueCount];

    createLocalQueue(localAppQueues[localQueueCount], queueName);
    localQueueCount++;

    And "localAppQueues" is declared in the header file as: qc::LocalQueue* localAppQueues[100];

    In another method, when I try to use one of the stored object pointers (e.g. "qc::Message m = localAppQueues[i]->get()", I get an Access Violation.

    Is there an obvious issue with how I am creating/passing the pointers that is causing this?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Array of Object Pointers

    No one knows what the values of those variables you're using are. We don't know when, where, and how those functions are called, or what context they're called in, etc..

    Runtime errors involving memory handling requires us to have the code, compile it ourselves, and run it with the data that you're using. Otherwise, the only thing that anyone can tell you is to check your indices and memory overwrites.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Feb 2009
    Posts
    4

    Re: Array of Object Pointers

    To compile and run this would take more time than I would expect anyone to spend on it.

    But maybe I can narrow it down. When I do not pass the pointer to the createLocalQueue method, I am able to use it without the Access Violation.

    This is successful:


    localAppQueues[localQueueCount] = new qc::LocalQueue;

    createLocalQueue(localAppQueues[localQueueCount], queueName);

    //qc::SubscriptionManager subs(session);
    //subs.subscribe(*localAppQueues[localQueueCount], queueName);

    qc::Message m = localAppQueues[localQueueCount]->get();

    And this has the runtime memory error:


    localAppQueues[localQueueCount] = new qc::LocalQueue;

    //createLocalQueue(localAppQueues[localQueueCount], queueName);

    qc::SubscriptionManager subs(session);
    subs.subscribe(*localAppQueues[localQueueCount], queueName);

    qc::Message m = localAppQueues[localQueueCount]->get();

    Is it possible that I am changing the pointer by passing it into the createLocalQueue method? It appears to me that moving the method's inline does not change anything.

  4. #4
    Join Date
    Feb 2009
    Posts
    4

    Re: Array of Object Pointers

    My apologies, in the previous post I had reversed the code segments. The first throws an error and the second (without the method call) is successful.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Array of Object Pointers

    Quote Originally Posted by khayman218 View Post
    To compile and run this would take more time than I would expect anyone to spend on it.
    The point is that runtime errors cannot be debugged by individuals looking at two or three lines of code from a much larger program. Add to that, no knowledge of the values of those variables or in what state your program is in by the time it reaches the crash point. We also don't know what those functions you're calling are doing internally.
    But maybe I can narrow it down. When I do not pass the pointer to the createLocalQueue method, I am able to use it without the Access Violation
    Your errors could have occurred much sooner than the code you're looking at. That code is where the application finally breaks down, but it doesn't guarantee that is the origin of the error.
    Is it possible that I am changing the pointer by passing it into the createLocalQueue method? It appears to me that moving the method's inline does not change anything.
    You cannot "change the pointer" unless you pass a pointer to the pointer you're changing. You are not doing that.

    Secondly, trying random things here and there is not how you solve these issues. You have to know why something doesn't work, and then fix it. Otherwise, what you'll end up doing is changing the code around so much that the error is moved to another part of the code, maybe a part of the code that isn't executed, giving a false sense that the problem is fixed.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 6th, 2010 at 12:32 PM.

  6. #6
    Join Date
    Jul 2005
    Location
    Netherlands
    Posts
    2,042

    Re: Array of Object Pointers

    Quote Originally Posted by khayman218 View Post
    My apologies, in the previous post I had reversed the code segments. The first throws an error and the second (without the method call) is successful.
    First of all, please use code tags when posting code.

    The difference between the two pieces of code you posted seems to be the scope of the 'subs' variable. In the former, it is destroyed when exiting the function createLocalQueue, whereas in the latter it is destroyed later. That could give you a hint at where to look... or it could be incidental.
    As Paul explained, with the information you have provided it's more of a guessing game than anything else.
    Cheers, D Drmmr

    Please put [code][/code] tags around your code to preserve indentation and make it more readable.

    As long as man ascribes to himself what is merely a posibility, he will not work for the attainment of it. - P. D. Ouspensky

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured