I understand what the error means but it’s quite strange I couldn’t possibly think of any reasons,
Here is my simplified structure:
public partial class mainForm : Form
{
public mainForm()
{
InitializeComponent();
IgnoreList = new SortedSet<string>();
IgnoreListQueue IgnoreQueue = new IgnoreListQueue();
}
public class IgnoreListQueue
{
private Dictionary<string, int> myQueue;
public void Add(string s)
{
}
public IgnoreListQueue()
{
myQueue = new Dictionary<string, int>();
}
public bool contains()
{}
~IgnoreListQueue()
{
}
}
public SortedSet<string> IgnoreList;
public IgnoreListQueue IgnoreQueue;
public int LowLevelKeyboardProc(int nCode, IntPtr wParam, IntPtr lParam)
{
//IgnoreList is fine here
//IgnoreQueue is null here.
//Example:
// bool boo = IgnoreQueue.contains(some string);
}
}
In LowLevelKeyboardProc() function, IgnoreQueue was seen as null, and when VS debugged the crash, it really showed IgnoreQueue a null pointer..
As my program hooks Keyboard strokes so I wasn’t able to set breakpoint in the LowLevelKeyboardProc() function. However, I was able to breakpoint in the mainForm() constructor and showed that IgnoreQueue was indeed initialized and loaded some data at that point.
Any ideas please ?
This is just a scope issue. IgnoreQueue declared within the mainform() constructor and it is not available in the LowLevelKeyboardProc() method. But, IgnoreList is declared in the global level seems and initialized in the constructor.