-3

I have a class _PDevice which is implemented in PDevice.cpp and declared in PDevice.h

Also, in PDevice.h, I have added:

typedef QSharedPointer<_PDevice>    DDevice;

Now, there is another class QLDevice which inherits _PDevice

QLDevice also has a public member:

QFile*      m_file;

In another file Control.h, I have declared:

DDevice m_device;

And in the file Control.cpp, in a method I have added:

m_device = DDevice(new QLDevice(filePath)); 

This sets m_file in QLDevice constructor:

m_file = &filePath;

After this when I try to call m_file->close() it throws an error "Unhandled exception at 0x740DCB49 in QXDM.exe: 0xC0000005: Access violation executing location 0x00000000 "

Like this:

if (m_file == NULL)
        m_lastError = FCLOSE_NULL_ERR;
else
        m_file->close();

I am not understanding where am I making the mistake.

Everything goes fine until I call m_file->close()

Could somebody please help me on this.

Vishal Khemani
  • 171
  • 2
  • 13
  • Have you tried using a debugger? Anyway, without all relevant code, you can't prove that any other part of your program isn't invoking undefined behaviour & making all attempts at diagnosis meaningless. See "*Questions seeking debugging help ("**why isn't this code working?**") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it* in the question itself. *Questions without **a clear problem statement** are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve).*" – underscore_d Aug 10 '16 at 11:23
  • `type casting is going somewhere wrong` Why do you think type casting has anything to do with it? The quoted error makes no mention of that. Are we meant to guess what you mean? Were you just guessing? Does `DDevice(new QLDevice(filePath))` perform casting? Show the declarations and inheritance relationships of both `DDevice` and `QLDevice`. – underscore_d Aug 10 '16 at 11:26
  • `m_file = &filePath;` Show us what type `filePath` is and where its value/address comes from. I could go on, asking for one piece of missing info at a time... but why don't you just debug it yourself and/or post all the code. – underscore_d Aug 10 '16 at 11:31
  • My educated guess would be, `filePath` has been destroyed some time ago, leaving `m_file` a dangling pointer. – Igor Tandetnik Aug 10 '16 at 14:25

1 Answers1

0
QFile*      m_file

this member is not initialised, so you should check where it's created and that an appropriate initialisation method is called.

evilruff
  • 3,947
  • 1
  • 15
  • 27
  • m_file is not null. I am checking for it befor calling m_file->close() – Vishal Khemani Aug 10 '16 at 11:14
  • 3
    @VishalKhemani hence why you should show us all your code, so that you don't waste everyone's time with a bunch of irrelevant questions about details you didn't bother to show us. – underscore_d Aug 10 '16 at 11:25
  • I didnt said it's NULL, however according to exception I can assume it's NULL, I said - uninitialised. – evilruff Aug 10 '16 at 11:37