Hi Guys,

I'm getting a segmentation fault in my code and I'm not sure how to fix it. I know where it's occurring, and I know it has something to do with the way I'm using a double pointer, but just have been stuck on what to do. The program is large and the problem hard to explain, so I'm going to try and be as clear as possible and include all necessary pieces of code, however I don't think I can put all of it because it's too big. The problem is occuring in this stretch fo code:

noaMux **nm;
if (searchNoaMuxes(misrChainVector, scopeString, nm))
{
        for (int i = (*nm)->getLength()-1; i >= 0; i--)
        {
                 int signalNumberStartIndex, signalNumberEndIndex, signalNumber;
                 signalNumber = i;
                 (*nm)->setSignalNumber(i, signalNumber);
                 getline(*oldVcd, *lineString);
         }
}

and here is searchNoaMuxes"

bool searchNoaMuxes(vector<misrChain*> *misrChainVector, string scopeString, noaMux **nm)
{
	for (int i = 0; i < misrChainVector->size(); i++)
	{
		if (misrChainVector->at(i)->findNoaMux(scopeString, nm))
		{
			cout << "&" << endl;
			return true;
		}
	}

	return false;
}

and here is the class with "findNoaMux":

bool findNoaMux(string scopeString, noaMux **nm)
{
         for (int i = 0; i < misrs.size(); i++)
         {
             if (misrs.at(i)->getName() == scopeString)
             {
                     cout << 2 << endl;
                     cout << "*" << i << "*" << endl;
                     cout << name << endl;
                      *nm = misrs.at(i); //SEGMENTATION FAULT
                      cout << 3 << endl;
                      return true;
             }
         }

          return false;
}

It's odd because the segmentation fault occurs at the commented line above, but only happens at the end of the foreoop, seemingly after the pointer has already been assigned and the for loop has run every iteration except the last. Does anyone have any idea what is causing this? BTW I allocated all the memory in this method, which is not main, in a block of code that looks like this (except longer):

csib8 = new misrChain("csimisr_b8_counter");
csib8->addNoaMux(new noaMux("/top_tb/mch_imc/umch/usmt/smtcsi1/hctlunit1/hctlnoa1/tlnoafub1/moamux1", 128));
csib8->addNoaMux(new noaMux("/top_tb/mch_imc/umch/usmt/smtcsi1/hctlunit1/hctlnoa1/tlnoafub2/moamux1", 128));

Any help would be appreciated, thanks.

bleh

If it's the same code, you have obviously uninitialized nm pointer to pointer:

noaMux **nm;
if (searchNoaMuxes(misrChainVector, scopeString, nm))
{
        for (int i = (*nm)->getLength()-1; i >= 0; i--)
...

findNoaMux uses it then crashed...

If it's the same code, you have obviously uninitialized nm pointer to pointer:

noaMux **nm;
if (searchNoaMuxes(misrChainVector, scopeString, nm))
{
        for (int i = (*nm)->getLength()-1; i >= 0; i--)
...

I've also tried initializing it to NULL and I still get the same error. The weird thing is, is that the object is retrieved, and goes through the for loop setting signal numbers for that object, however just before the last iteration of the loop, it crashes, and when I look in gdb it tells me the crash is occurring in findNoaMux at the line where the pointer is assigned, which seemingly is happening. I wonder if this has to do with memory deallocation?

Bleh

Probably, you want:

noaMux *nm; // pointer initialized via parameter
if (searchNoaMuxes(misrChainVector, scopeString, &nm))
{
   ...

If so, better pass a reference to, not a pointer...

ArkM,

That worked! Thanks! Btw, do you know if I need to garbage college the dynamic memory I'm allocating?

Bleh

Yes, of course - but not "garbage collect" term used: you must deallocate these objects by delete nm.
That's why this style is not so good: you create objects in the depth and it's not so clear where you need delete them. One morning after the next code refactoring you will pass a pointer to local or global (not allocated by operator new) object then try to delete it. Error prone approach...

Good luck!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.