How to fix COMPtr' problem?

How to fix COMPtr' problem?

D_D_K
Advocate Advocate
698 Views
4 Replies
Message 1 of 5

How to fix COMPtr' problem?

D_D_K
Advocate
Advocate

Hi,

After running the following code, my cad crashes at the end.

because there are some custom functions with parameter IAcadBlocksPtr, so I used it like below: 

 

CComQIPtr<IAcadApplication> pApp(acedGetIDispatch(FALSE));
CComPtr<IAcadDocument> pDoc;  //getcurrent doc

IAcadBlocksPtr pblks;
hR = pDoc->get_Blocks(pblks); 

//do something Ok

pblks.Release();

HRESULT hr = pDoc->Close(vb); //Ok
pDoc.Release();

return true;// <---crash;

 

image.png

 

How do I avoid this?

0 Likes
Accepted solutions (1)
699 Views
4 Replies
Replies (4)
Message 2 of 5

Alexander.Rivilis
Mentor
Mentor

1. pDoc is not initilaize

2. pDoc release twice as far as you call pDoc.Release() and destructor of pDoc also call pDoc.Release()

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 3 of 5

D_D_K
Advocate
Advocate

Hi sir,

the reason I call pDoc.release(); because when using the original code below -> cad crashes first.

I'm not sure where is wrong. these variables are initialized and working correctly.

 

 

CComQIPtr<IAcadApplication> pApp(acedGetIDispatch(FALSE));
CComPtr<IAcadDocument> pDoc;  
pApp->get_ActiveDocument(&pDoc);

IAcadBlocksPtr pblks;
hR = pDoc->get_Blocks(pblks); 

//do something Ok

pblks.Release();
HRESULT hr = pDoc->Close(vb); //success

return true;// <---crash;

 

 

0 Likes
Message 4 of 5

Alexander.Rivilis
Mentor
Mentor
Accepted solution

Are you trying to close active document?

It is look like error not in this code.

Відповідь корисна? Клікніть на "ВПОДОБАЙКУ" цім повідомленням! | Do you find the posts helpful? "LIKE" these posts!
Находите сообщения полезными? Поставьте "НРАВИТСЯ" этим сообщениям!
На ваше запитання відповіли? Натисніть кнопку "ПРИЙНЯТИ РІШЕННЯ" | Have your question been answered successfully? Click "ACCEPT SOLUTION" button.
На ваш вопрос успешно ответили? Нажмите кнопку "УТВЕРДИТЬ РЕШЕНИЕ"


Alexander Rivilis / Александр Ривилис / Олександр Рівіліс
Programmer & Teacher & Helper / Программист - Учитель - Помощник / Програміст - вчитель - помічник
Facebook | Twitter | LinkedIn
Expert Elite Member

0 Likes
Message 5 of 5

D_D_K
Advocate
Advocate

Thanks sir, 

I have reviewed my code. the error comes from a subroutine.

Remove it and now the code works fine.

0 Likes