C++ pointers and DLL linking
-
Hello, I'm David. Recently I've been working on a project... A robot that can be controlled locally by a Xbox360 controller, and eventually will be controlled remotely the same way. To control the motors on the robot I have to link with a DLL, and so far it works for a bit, but soon after starting the app it is closed for Access Violations. This issue deffinately envolves the definitions and pointers and such to the DLL... What is going wrong, and how can i prevent it from happening? Thanks, David Kirby /*-----------------------------------------------------------------------------------------------*/ #include "CXBOXController.h" #include <iostream> #include <windows.h> #include <tchar.h> #include <cstdlib> typedef bool (*Type_InitMotoBee)(); typedef bool (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void (*Type_Digital_IO)(int *inputs, int outputs); using namespace std; HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); CXBOXController* Player1; int main(int argc, char* argv[]) { Player1 = new CXBOXController(1); system("Color 02"); system("title LANbot Host - Local Motor Control Trial"); cout << " __ _ __ _ _ |\n" << " / / /_\\ /\\ \\ \\ |__ ___ | |_ |\n" << " / / //_\\\\ / \\/ / '_ \\ / _ \\| __| |\n" << " / /___/ _ \\/ /\\ /| |_) | (_) | |_ |\n" << " \\____/\\_/ \\_/\\_\\ \\/ |_.__/ \
-
Hello, I'm David. Recently I've been working on a project... A robot that can be controlled locally by a Xbox360 controller, and eventually will be controlled remotely the same way. To control the motors on the robot I have to link with a DLL, and so far it works for a bit, but soon after starting the app it is closed for Access Violations. This issue deffinately envolves the definitions and pointers and such to the DLL... What is going wrong, and how can i prevent it from happening? Thanks, David Kirby /*-----------------------------------------------------------------------------------------------*/ #include "CXBOXController.h" #include <iostream> #include <windows.h> #include <tchar.h> #include <cstdlib> typedef bool (*Type_InitMotoBee)(); typedef bool (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void (*Type_Digital_IO)(int *inputs, int outputs); using namespace std; HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); CXBOXController* Player1; int main(int argc, char* argv[]) { Player1 = new CXBOXController(1); system("Color 02"); system("title LANbot Host - Local Motor Control Trial"); cout << " __ _ __ _ _ |\n" << " / / /_\\ /\\ \\ \\ |__ ___ | |_ |\n" << " / / //_\\\\ / \\/ / '_ \\ / _ \\| __| |\n" << " / /___/ _ \\/ /\\ /| |_) | (_) | |_ |\n" << " \\____/\\_/ \\_/\\_\\ \\/ |_.__/ \
This would be too much work for somebody trying to help you. It is for me. So you should consider drilling this down to a more specific area. These are some comments from me. Don't use
new
unless you really need it. You are using too manysystem
function calls. The code is not properly indented. You are not checking return values for errors.«_Superman_» I love work. It gives me something to do between weekends.
-
This would be too much work for somebody trying to help you. It is for me. So you should consider drilling this down to a more specific area. These are some comments from me. Don't use
new
unless you really need it. You are using too manysystem
function calls. The code is not properly indented. You are not checking return values for errors.«_Superman_» I love work. It gives me something to do between weekends.
Thanks for the reply. Im getting and error with these... typedef bool (*Type_InitMotoBee)(); typedef bool (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void (*Type_Digital_IO)(int *inputs, int outputs); HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); Type_InitMotoBee InitMotoBee; Type_SetMotors SetMotors; Type_Digital_IO Digital_IO; InitMotoBee = (Type_InitMotoBee)GetProcAddress(MHtbDLL, "InitMotoBee"); SetMotors = (Type_SetMotors)GetProcAddress(MHtbDLL, "SetMotors"); Digital_IO = (Type_Digital_IO)GetProcAddress(MHtbDLL, "Digital_IO"); This is where the program calls the DLL, makes pointers, etc. After I use SetMotors(); so many times, the program is terminated for an access violation... I cant figure it out. If i comment out everything that envolves the DLL, my program works perfectly. Thanks, David
-
Thanks for the reply. Im getting and error with these... typedef bool (*Type_InitMotoBee)(); typedef bool (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void (*Type_Digital_IO)(int *inputs, int outputs); HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); Type_InitMotoBee InitMotoBee; Type_SetMotors SetMotors; Type_Digital_IO Digital_IO; InitMotoBee = (Type_InitMotoBee)GetProcAddress(MHtbDLL, "InitMotoBee"); SetMotors = (Type_SetMotors)GetProcAddress(MHtbDLL, "SetMotors"); Digital_IO = (Type_Digital_IO)GetProcAddress(MHtbDLL, "Digital_IO"); This is where the program calls the DLL, makes pointers, etc. After I use SetMotors(); so many times, the program is terminated for an access violation... I cant figure it out. If i comment out everything that envolves the DLL, my program works perfectly. Thanks, David
Check for any uninitialized or dangling pointers inside SetMotors. Check if there is any difference in behavior in Debug and Release builds. Also set the warning level to the highest level in the project properties.
«_Superman_» I love work. It gives me something to do between weekends.
-
Check for any uninitialized or dangling pointers inside SetMotors. Check if there is any difference in behavior in Debug and Release builds. Also set the warning level to the highest level in the project properties.
«_Superman_» I love work. It gives me something to do between weekends.
How exactly would i Check for dangling pointers in SetMotors? If its any help, I can tell you that When i run the app in 32bit windows xp OS, the app closes after two successful SetMotors(); commands. In 64bit windows vista OS desktop, the app runs well for quite a while before closing for the same reason. I'm checking for release/debug differences now.
-
How exactly would i Check for dangling pointers in SetMotors? If its any help, I can tell you that When i run the app in 32bit windows xp OS, the app closes after two successful SetMotors(); commands. In 64bit windows vista OS desktop, the app runs well for quite a while before closing for the same reason. I'm checking for release/debug differences now.
Are you allocating memory dynamically? If so, check if they are being released properly. Also, check for buffer overflows. If you're using string functions, use the secure version that end with _s.
«_Superman_» I love work. It gives me something to do between weekends.
-
Are you allocating memory dynamically? If so, check if they are being released properly. Also, check for buffer overflows. If you're using string functions, use the secure version that end with _s.
«_Superman_» I love work. It gives me something to do between weekends.
Sorry, I'm new at including DLL's. I have seen "Buffer Overflow" show up as an error, but from what i can tell I fixed that. I'm pretty sure that when that occured, the compiler (Visual Studio 2008) failed to compile the app and gave me the buffer overflow error. that is no longer the case. I am not allocating memory dynamically for the DLL. there is no difference between Release and Debug. It is handling itself after I call the DLL, make the pointers, and GetProcAddress. I just use the functions... the functions are used as follows: SetMotors(x,x,x,x,x,x,x,x,x) InitMotoBee();
-
Hello, I'm David. Recently I've been working on a project... A robot that can be controlled locally by a Xbox360 controller, and eventually will be controlled remotely the same way. To control the motors on the robot I have to link with a DLL, and so far it works for a bit, but soon after starting the app it is closed for Access Violations. This issue deffinately envolves the definitions and pointers and such to the DLL... What is going wrong, and how can i prevent it from happening? Thanks, David Kirby /*-----------------------------------------------------------------------------------------------*/ #include "CXBOXController.h" #include <iostream> #include <windows.h> #include <tchar.h> #include <cstdlib> typedef bool (*Type_InitMotoBee)(); typedef bool (*Type_SetMotors)(int on1, int speed1, int on2, int speed2, int on3, int speed3, int on4, int speed4, int servo); typedef void (*Type_Digital_IO)(int *inputs, int outputs); using namespace std; HINSTANCE MHtbDLL=LoadLibrary(_T("mtb.dll")); CXBOXController* Player1; int main(int argc, char* argv[]) { Player1 = new CXBOXController(1); system("Color 02"); system("title LANbot Host - Local Motor Control Trial"); cout << " __ _ __ _ _ |\n" << " / / /_\\ /\\ \\ \\ |__ ___ | |_ |\n" << " / / //_\\\\ / \\/ / '_ \\ / _ \\| __| |\n" << " / /___/ _ \\/ /\\ /| |_) | (_) | |_ |\n" << " \\____/\\_/ \\_/\\_\\ \\/ |_.__/ \
It seems that the access violation is due to Dangling pointers. These kind of error will not be repeatable 100% ,since will be depending on size of your RAM ( when your OS tries to get back the memory which is assigned to some one else).If you have Dev-Partner with your studio , you can find the code problems or try to make sure that there is delete() for every new().You can use run time checker for this task , It's evaluation version may help you in Finding leaks.
It's not enough to be the best, when you have capability to be great....