D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 10622 - Wrong "this" pointer in methods called via IUnknown derived interfaces
Summary: Wrong "this" pointer in methods called via IUnknown derived interfaces
Status: NEW
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: x86_64 Windows
: P2 major
Assignee: No Owner
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2013-07-12 07:52 UTC by Sönke Ludwig
Modified: 2024-12-13 18:09 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Sönke Ludwig 2013-07-12 07:52:57 UTC
The following program crashes in the second invocation of AddRef (could be any other method defined by IUnknown or a derived interface) because the "this" pointer points to the IUnknown vtable instead of the object's base, which is assumed by the code.

---
import std.c.windows.com;
import std.c.windows.windows;

class Test : IUnknown {
  int i = 1;
  ULONG AddRef() { assert(i == 1); return 0; }
  ULONG AddRef() { assert(i == 1); return 0; }
  HRESULT AddRef(IID*, void**) { assert(i == 1); return E_FAIL; }
}

void main()
{
  auto t = new Test;
  t.AddRef(); // works
  auto u = cast(IUnknown)t;
  u.AddRef(); // crash in _d_invariant
}
---

Setting this to major severity because it makes defining any COM objects in D impossible on Win64 (and thus many COM APIs are unusable). Tested on DMD 2.063.2
Comment 1 Sönke Ludwig 2013-07-12 08:50:03 UTC
Actually, as far as I understand, the pointer is correct and the function body is wrong in assuming that it points to the object's base.
Comment 2 dlangBugzillaToGithub 2024-12-13 18:09:17 UTC
THIS ISSUE HAS BEEN MOVED TO GITHUB

https://github.com/dlang/dmd/issues/18629

DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB