ObjectARX
cancel
Showing results forĀ 
ShowĀ Ā onlyĀ  | Search instead forĀ 
Did you mean:Ā 

Run-Time Check Failure #2 - Stack around the variable 'name'' was corrupted

7 REPLIES 7
SOLVED
Reply
Message 1 of 8
Chaos0205
2310 Views, 7 Replies

Run-Time Check Failure #2 - Stack around the variable 'name'' was corrupted

I got this Error upon return the following code. Anyone can tell me why and how to fix it please ? Thanks in advance

 

{

        AcDbLayerTable* pLayerTbl = NULL;
        acdbCurDwg()->getSymbolTable(pLayerTbl,AcDb::kForWrite);
        AcDbLayerTableRecord* pBTR = new AcDbLayerTableRecord();
        AcCmColor color;
        int index;
        acedInitGet(RSG_NONULL && RSG_NOZERO && RSG_NONEG, NULL);
        acedGetInt(_T("\n Input layer color "), &index);
        color.setColorIndex(index);
        pBTR->setColor(color);
        pBTR->close();
        ACHAR name;
        acedGetString(0,_T("\n Input layer name "), &name);
        pBTR->setName(&name);
        pLayerTbl->add(pBTR);
        pBTR->close();
        pLayerTbl->close();

}

  
7 REPLIES 7
Message 2 of 8
owenwengerd
in reply to: Chaos0205

The 'name' buffer needs to have enough space to hold the entered string.

--

Owen Wengerd

ManuSoft

--
Owen Wengerd
ManuSoft
Message 3 of 8
Chaos0205
in reply to: owenwengerd

But there is not command like name.buffer nor name->buffer. How can i set the buffer long enough to solve this? Beside, this still crash even if the name i entered is 1 character long

Message 4 of 8
owenwengerd
in reply to: Chaos0205

The only one-character string is the empty string, because all C strings are null terminated.

 

To allocate name on the stack with a fixed size:

ACHAR name[134];

--

Owen Wengerd

ManuSoft

 

--
Owen Wengerd
ManuSoft
Message 5 of 8
Chaos0205
in reply to: owenwengerd

It is still not working thought :(. I have changed my code like you said, but the problem remain

My current code (the rest is unchanged šŸ˜ž

// ....

        ACHAR name[100];
        acedGetString(0,_T("\n Input layer name "), &name[100]);
        pBTR->setName(&name[100]);

// ....

 

Message 6 of 8
owenwengerd
in reply to: Chaos0205

You're sending a pointer to the last character in the allocated buffer, which makes it effectively a one-character buffer. Use all of the buffer, starting from the beginning (and note that a 100 character buffer is not large enough -- it must be at least 134):

 

ACHAR name[134];

acedGetString(0,_T("\n Input layer name "), name);

//need to check return value in case there was an error

pBTR->setName(name);

--

Owen Wengerd

ManuSoft

 

--
Owen Wengerd
ManuSoft
Message 7 of 8
Chaos0205
in reply to: owenwengerd

It worked ! Thank you very much !

Message 8 of 8
khunstad
in reply to: Chaos0205

I came over this topic searching for a problem. And I noticed that you are first closing pBTR, then trying to change it! You should remove the first pBTR->close(); I think Smiley Happy

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

ā€Boost