Недавно добавленные исходники

•  DeLiKaTeS Tetris (Тетрис)  172

•  TDictionary Custom Sort  3 344

•  Fast Watermark Sources  3 095

•  3D Designer  4 853

•  Sik Screen Capture  3 350

•  Patch Maker  3 556

•  Айболит (remote control)  3 665

•  ListBox Drag & Drop  3 020

•  Доска для игры Реверси  81 746

•  Графические эффекты  3 948

•  Рисование по маске  3 253

•  Перетаскивание изображений  2 633

•  Canvas Drawing  2 761

•  Рисование Луны  2 586

•  Поворот изображения  2 196

•  Рисование стержней  2 172

•  Paint on Shape  1 570

•  Генератор кроссвордов  2 241

•  Головоломка Paletto  1 771

•  Теорема Монжа об окружностях  2 238

•  Пазл Numbrix  1 685

•  Заборы и коммивояжеры  2 059

•  Игра HIP  1 282

•  Игра Go (Го)  1 232

•  Симулятор лифта  1 477

•  Программа укладки плитки  1 219

•  Генератор лабиринта  1 549

•  Проверка числового ввода  1 368

•  HEX View  1 497

•  Физический маятник  1 359

 
скрыть


Delphi FAQ - Часто задаваемые вопросы

| Базы данных | Графика и Игры | Интернет и Сети | Компоненты и Классы | Мультимедиа |
| ОС и Железо | Программа и Интерфейс | Рабочий стол | Синтаксис | Технологии | Файловая система |



Delphi Sources

Указатель на вариантный тип - предупреждение



Оформил: DeeCo

(deutsche Version siehe unten)
I just ran across this in some of my old code and thought I''d share it with you:

Consider the following (simplified code):
 function CreateVariantPtr(_Value: variant): pVariant;
 begin
 GetMem(Result, SizeOf(Variant));
 Result^ := _Value;
 end;
Seems pretty simple, doesn''t it? Anybody who can spot the bug? I got spurious access violations in the line Result^ := _Value; Ok, the reason: Memory allocated with GetMem is not initialised, so the "Variant" pointed to by Result contains some random data, for example something that might represent a variant type which requires some finalisation when the variant is changed, lets say a variant array of some sort. But this finalisation won''t work because the content is just some random garbage, and sometimes I was lucky (I mean that!) and got an access violation that allowed me to spot the error. The fix:
 function CreateVariantPtr(_Value: variant): pVariant;
 begin
 GetMem(Result, SizeOf(Variant));
 Initialize(Result^);
 Result^ := _Value;
 end;

 {***********--- deutsch ---***********************}
Mir ist gerade folgendes in ein alten Sourcen aufgefallen und ich dachte, das interessiert den einen oder anderen.. Folgender Code (vereinfacht):
 function CreateVariantPtr(_Value: variant): pVariant;
 begin
 GetMem(Result, SizeOf(Variant));
 Result^ := _Value;
 end;
Sieht ziemlich simpel aus, oder? Kann jemand den Bug finden? Ich bekam manchmal Access Violations in der Zeile. Result^ := _Value; Der Grund: Wenn man Speicher mit GetMem anfordert, so wird dieser nicht initialisiert, d.h. das der "Variant", auf den Result zeigt, voellig zufaelligen Inhalt hat, es koennte z.B. eine Variant-Typ sein, der, wenn er ueberschrieben wird, irgendwie freigegeben werden muss, z.B. ein Variant Array. Aber das Freigeben wuerde fehlschlagen, denn es ist ja nicht wirklich ein Variant Array sondern nur Speichermuell, der zufaellig so aussieht wie ein solches. Und ich hatte das Glueck (und ich meine Glueck) dass ich eine Access Violation bekam, die mich auf diesen Fehler hinwies (die Alternative ist, dass der Freigabecode irgendwo im Speicher rumbastelt und dabei moeglicherweise wichtige Daten veraendert, ohne dass ich das merke, so mit der am schwierigsten zu findende Bug, den es gibt). Der Bugfix:
 function CreateVariantPtr(_Value: variant): pVariant;
 begin
 GetMem(Result, SizeOf(Variant));
 Initialize(Result^);
 Result^ := _Value;
 end;







Copyright © 2004-2024 "Delphi Sources" by BrokenByte Software. Delphi World FAQ

Группа ВКонтакте