Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Dynamically allocate memory

Posted on 2016-09-02
9
Medium Priority
?
94 Views
Last Modified: 2016-09-03
Hi Guys.

I'm not a C++ developer and with this project I'm doing I was forced to use C++. The problem is that when I run my app and try to open a service its says Access violation reading location 0x00000000. The pointer is
lphService

Open in new window

I've looked around on how to allocate memory but I don't seem to understand as to how could I assign this as the size will be returned by a function and I don't know the size by head. Any help?
0
Comment
Question by:Mlungisi Ndlela
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
9 Comments
 
LVL 31

Expert Comment

by:Zoppo
ID: 41781174
Hi MCSI Developers,

in general the error occurs in case a NULL-pointer is dereferenced, in your case the program tries to access (read) the value where the pointer lphService points to.

We need more info about this to help you, could you post some code (especially all parts where the lphService is instanced and used)?

Best regards,

ZOPPO
0
 
LVL 2

Author Comment

by:Mlungisi Ndlela
ID: 41781205
@ZOPPO, here is the code, and I get error -26.

    LPHSERVICE lphService;
    lphService = 0; // I put 0 because the EXIT_SUCCESS is also 0
    HRESULT hResult;
    WFSVERSION SvcVersion, SpiVersion;
    char szLogicalName[]="Epson1200";
    hResult = (WFSOpen(szLogicalName,WFS_DEFAULT_HAPP,"Epson Print",WFS_TRACE_NONE,TWO_MINUTES,RECOGNISED_VERSIONS,&SvcVersion,&SpiVersion,lphService));
    if(hResult == WFS_ERR_CANCELED)
    {
    cout << WFS_ERR_CANCELED << endl; // This returns -4
    }else if(hResult == WFS_ERR_SERVICE_NOT_FOUND)
    {
    cout << WFS_ERR_SERVICE_NOT_FOUND << endl; // This is -43
    }else if(hResult == WFS_ERR_CONNECTION_LOST)
    {
    cout << WFS_ERR_CONNECTION_LOST << endl; // -54
    }else if(hResult == WFS_ERR_INTERNAL_ERROR)
    {
    cout << WFS_ERR_INTERNAL_ERROR << endl; // -15
    }else if(hResult == WFS_ERR_INVALID_APP_HANDLE)
    {
    cout << WFS_ERR_INVALID_APP_HANDLE << endl; // -17
    }else if(hResult == WFS_ERR_INVALID_POINTER)
    {
    cout << WFS_ERR_INVALID_POINTER << endl; // This is the error I get which is -26
    }

Open in new window

0
 
LVL 31

Accepted Solution

by:
Zoppo earned 2000 total points
ID: 41781716
ok, I guess the LPHSERVICE is a typedef to a HSERVICE pointer, so you should be able to solve it using one of these methods:

1. Use a pointer: change line 2 to
lphService = new HSERVICE;

Open in new window

and add this line after the last use of lphservice:
delete lphservice;

Open in new window


2. Use an instance: change line 1 to
HSERVICE hService;

Open in new window

delete line 2 and replace lphService by &hService in line 6.

In both cases the pointer passed to WSFOpen points to a valid instance, with 1. this instance is created in heap (and therefor has to be released after last use), with 2. the instance is put onto the stack, thus released automatically when the function terminates.

Hope this helps,

ZOPPO
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 31

Expert Comment

by:Zoppo
ID: 41781726
BTW, in addition two hints:

1. you don't need the ( and ) surrounding the WSFOpen call
2. using a chain of if/else to check a variable against constant values is not optimal for performance, in your case a switch/case statement would perform better.

ZOPPO
0
 
LVL 2

Author Closing Comment

by:Mlungisi Ndlela
ID: 41782249
Thanks thats really helped, now I get this error: WFS_ERR_SERVICE_NOT_FOUND. Is this because of missing device or the name is not found on the registry?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 41782922
Hm - sorry, that's hard to say for me since I don't know the API you're using - you could try if the program acts different when you dis- and re-connect the device, if so I would guess it's a problem with an unregistered (in registry) service.
0
 
LVL 2

Author Comment

by:Mlungisi Ndlela
ID: 41782924
I would guess it's a problem with an unregistered (in registry) service
do you mean my App is not registered or the device/service?
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 41783110
I would guess the service is unregistered or it is registered but not running.
1
 
LVL 2

Author Comment

by:Mlungisi Ndlela
ID: 41783219
Thanks,
registered but not running
that does make seance.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
Suggested Courses

618 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question