![]() | |
| |||||||
| Home | Register | Blogs | FAQ | Members List | Calendar | Downloads | Arcade | Mark Forums Read |
| Languages, Compilers and Interpreters C,C++,C#,.NET,Java,PHP,Perl,SQL and more |
![]() |
| | LinkBack | Thread Tools | Display Modes |
| | #1 |
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,178
Rep Power: 7 Reputation:
45 | вот код: Code: struct B1 { };
struct B2 { };
struct D: B2, B1 { };
int main()
{
B1* b1 = new D;
delete b1;
B2* b2 = new D;
delete b2;
return 0;
} подсистема динамической памяти пытается найти соостветствующий адрес среди аллоцированных, и не находитподелитесь опытом борьбы с этим багом
__________________ Сайт армянских маньяков |
| | |
| | #2 |
| Студент Join Date: Jun 2002 Location: Yerevan
Posts: 258
Rep Power: 7 Reputation:
10 | Ok, just some info that may help. Debug pokazal, chto oshibka ne v new ili delete-e, a v operatore = (tochnee v casting operatore), posle new on poluchaet pravil'nyj address, no kogda = delaet, pribavlyaet k addressu odin byte (ochevidno eto kak-to svyazanno s tem chto on na vtorom meste v nasledovanii) i vse letit kuda podal'she. Oshibka ochen' weird, esli najdesh' solution, obyazatel'no postni!
__________________ http://www.d-brane.com |
| | |
| | #3 |
| Студент Join Date: Jun 2002 Location: Yerevan
Posts: 258
Rep Power: 7 Reputation:
10 | Nu da zabyl pro obvious solution: B1* b1 = ( B1* )( void* )new D;
__________________ http://www.d-brane.com |
| | |
| | #4 |
| Студент Join Date: Jun 2002 Location: Yerevan
Posts: 258
Rep Power: 7 Reputation:
10 | Mogesh' esche esli mazoxizma mnogo napisat' casting operator ![]()
__________________ http://www.d-brane.com |
| | |
| | #5 | |
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,178
Rep Power: 7 Reputation:
45 | Quote:
__________________ Сайт армянских маньяков | |
| | |
| | #6 |
| Студент Join Date: Jun 2002 Location: Yerevan
Posts: 258
Rep Power: 7 Reputation:
10 | Daby dolgo ne perepinat'sya konkretnyj primer privedesh' plz, kogda eto ne budet rabotat'?
__________________ http://www.d-brane.com |
| | |
| | #7 | |
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,178
Rep Power: 7 Reputation:
45 | Quote:
) подобъектов. один из них находится в самом начале D, и поэтому его адрес совпадает с адресом обрамляющего объекта. второй же - чуть ниже, и адрес у него не равен D::this (это конечно условная запись, не надо компилить ни в уме, ни на машине). при new подсистема динамической памяти регистрирует выделяемый адрес, чтобы потом освободить. в данном случае это адрес D-объекта. а освобождаю я потом через тот базовый указатель, который не совпадает с D::this. подсистема ищет B1::this в куче и, конечно же, не находит. и выбрасывает assert.однако, на счастье всем интересующимся, я только что исправил положение. надо в обоих классах объявить виртуальные деструкторы (shame on me - в одном из них его не было! досаданая ошибка!). и тогда он добирается до корректного адреса при освобождении памяти (кстати, добирается именно через деструктор - объявление другой виртуальной функции с единственной целью - сгенерировать таблицу, не помогает)
__________________ Сайт армянских маньяков | |
| | |
| | #8 | |
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,178
Rep Power: 7 Reputation:
45 | Quote:
Code: struct B1 { };
struct B2 { };
struct D: B2, B1 { };
int main()
{
D* d = new D;
b1 = (B1*)(void*)d; fout << b1;
b1 = d; fout << b1;
fout << d;
delete d;
return 0;
} 002F0998 002F099C 002F0998 как видим, во втором и первом случае содержимые b1 различаются. в первом случае этот адрес совпадает с третьим случаем, т. е. b1 указывает ровно на d, хотя, как следует из второго случая, подобъект B1 на самом деле находится по другому адерсу
__________________ Сайт армянских маньяков | |
| | |
| | #9 |
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,178
Rep Power: 7 Reputation:
45 | bottom line: it wasn't a bug just my fault. так что поучитесь на моей ошибке ![]()
__________________ Сайт армянских маньяков |
| | |
| | #10 |
| Студент Join Date: Jun 2002 Location: Yerevan
Posts: 258
Rep Power: 7 Reputation:
10 | Ok, good Kogda compile delaesh' s virtual destructorami delaet delete sovershenno po-drugomu (dage ne zaxodit v delete, at least v c-code mode-e ).Just to have some more fun, another solution, without destructors: delete (D*)b1; ![]()
__________________ http://www.d-brane.com |
| | |
| | #11 |
| Студент Join Date: Jun 2002 Location: Yerevan
Posts: 258
Rep Power: 7 Reputation:
10 | btw s primerom tvoim ne slishkom soglasen T.e. on ne pokazal chto-to nevernoe, on prosto pokazal, chto razlichnye address-a, ok so what, moget/budet li ot etogo chto-libo menyatsya pri ispol'zovanii etogo ob'ekta? P.S. Ya ne govoryu chto ty ne prav, prosto esli state delaesh', dokagi ![]()
__________________ http://www.d-brane.com |
| | |
| | #12 | |
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,178
Rep Power: 7 Reputation:
45 | Quote:
__________________ Сайт армянских маньяков | |
| | |
| | #13 | |
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,178
Rep Power: 7 Reputation:
45 | Quote:
__________________ Сайт армянских маньяков | |
| | |
| | #14 | ||
| Студент Join Date: Jun 2002 Location: Yerevan
Posts: 258
Rep Power: 7 Reputation:
10 | Quote:
T.k. real'no new vozvraschaet imenno to chto ty vidish' cherez (void*) new D, tak chto xochu primer!! A naschet chastnogo primera totally agree Just some fun ![]()
__________________ http://www.d-brane.com | ||
| | |
| | #15 | |
| The Reloaded Join Date: Jan 2002 Location: behind the flesh and gelatinе of soft dull eyes
Posts: 3,178
Rep Power: 7 Reputation:
45 | Quote:
__________________ Сайт армянских маньяков | |
| | |