Why i can't access dynamic array value from class member function?

Hi, im trying to create static library with a class that uses dynamic array, and want to return value from array (TYPE *type). Why i can set the value and return it from function ""set()", and getting access violation error:

"Unhandled exception at 0x01042ca8 in test.exe: 0xC0000005: Access violation reading location 0x00000000."

from function "take()"? Please help

// test.h....
typedef struct TYPE{
int x;
}TYPE;

class test{
TYPE *type;
public:
int set(void);
int take(void);
};

// test.cpp .....

#include "test.h"

int test::set(void)
{
type = NULL;
type = new TYPE[1];
type[0].x=10;
return type[0].x;
}

int test::take(void)
{
return type[0].x;
}


i use this class in WinMain() function:

#include "windows.h"
#include "test.h"

#pragma comment (lib, "classtest.lib")

test newtest;

int WINAPI WinMain(HINSTANCE hInst.....)
{
...
...
...
// here i get no errors!!!...
newtest.set();


MSG msg;

while(TRUE)
{
while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

if(msg.message == WM_QUIT)
break;

DrawGLScene();// this is the function i'm calling and getting error!!!

}

return msg.wParam;
}

}
void DrawGLScene(GLvoid)
{

......
......
......
......
// here is where i get error message!!!
int c=newtest.take();

}

Sorry for psudocode. Everything else in my program seems to work fine except this function call, so i just put what i think makes sense to put...
apart from leaking memory from failing to delete your new'd "type", that code will work fine.

The error suggests you are trying to dereference a NULL pointer which probably means "type" isn't pointing to anything valid when you try to get "x".

You must have copied something wrong (or right?) when you posted this pseudocode. Post the actual code or post a smaller, compilable version that reproduces the problem.
You're asking for a void in the function declaration, but not passing it.
1
2
3
4
5
6
class test{
    TYPE *type;
public:
    int set(void); // Change to int set();
    int take(void); // Change to int take();
};
Well, i took out some stuff i dont need, and lounched the program, and it worked. So this confused me more. Still can't figure out where i was wrong... Thanks for your cooperation lads.

anyway thats how it looks now:

//test.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include "test.h"
#include <new>

int test::set(void)
{
	type = NULL;
	type = new TYPE[1];
	type[0].x=10;
	return type[0].x;
}

int test::take(void)
{
	//int *ret=&type[0].x;
	//int r=*ret;
	//return r;
	return type[0].x;
}


//application.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include  "test.h"

#pragma comment (lib, "Opengl32.lib")
#pragma comment (lib, "Glu32.lib")
#pragma comment (lib, "classtest.lib")

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);

void DrawGLScene(GLvoid);

HWND main;

test newtest;

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wc;


	ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = NULL;
    wc.lpszClassName = L"WindowClass";

    RegisterClassEx(&wc);


    main = CreateWindowEx(NULL,
                          L"WindowClass",
                          L"Editor",
                          WS_OVERLAPPEDWINDOW,
                          300, 300,
                          500, 500,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    
		ShowWindow(main,SW_SHOW);						
		
		int d=newtest.set();

		if (d==10)
		{
			MessageBox(NULL,L"set working!",L"INFO",MB_OK|MB_ICONEXCLAMATION);
		}
		
    
	MSG msg;

    while(TRUE)
    {
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if(msg.message == WM_QUIT)
            break;

		DrawGLScene();

    }
    return msg.wParam;
}


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	

    switch(message)
    {
		case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}

void DrawGLScene(GLvoid)                             
{
	
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         
	glLoadIdentity();                           
	
	int c=newtest.take();
	
	if (c==10)
	{
		MessageBox(NULL,L"take from gl working!",L"INFO",MB_OK|MB_ICONEXCLAMATION);
	}
	glFlush();
}



I'm probably just too tired after work today :)
Last edited on
A pretty common problem with global variables is that you can accidentally change the value to something that's not acceptable to other functions that use it. I firmly believe that there isn't any call to have a globally declared variable outside of a class.
No, there are no variables declared globally except class declaration before winmain. The code is simple as "glass of water". Any way, the error is gone and i dont know where... Thanks
test newtest; Global variable. Just because it's a variable type that you defined doesn't mean it's not a variable.
Finally i found where the problem was. I recovered the code before i removed some stuff from it and found that error was caused by the "DrawGLScene(GLvoid)" function call in window procedure (case WM_SIZING: and case WM_SIZE:). This is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <windows.h>
#include <gl\gl.h>
#include <gl\glu.h>
#include "test.h"

#pragma comment (lib, "Opengl32.lib")
#pragma comment (lib, "Glu32.lib")
#pragma comment (lib, "classtest.lib")

LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam);
void DrawGLScene(GLvoid);


HWND main;

test newtest;

int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wc;


	ZeroMemory(&wc, sizeof(WNDCLASSEX));

    wc.cbSize = sizeof(WNDCLASSEX);
    wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
    wc.lpfnWndProc = WindowProc;
    wc.hInstance = hInstance;
    wc.hCursor = LoadCursor(NULL, IDC_ARROW);
    wc.hbrBackground = NULL;
    wc.lpszClassName = L"WindowClass";

    RegisterClassEx(&wc);


    main = CreateWindowEx(NULL,
                          L"WindowClass",
                          L"Editor",
                          WS_OVERLAPPEDWINDOW,
                          300, 300,
                          500, 500,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

		ShowWindow(main, nCmdShow);
		
		int d=newtest.set();

		if (d==10)
		{
			MessageBox(NULL,L"set working!",L"INFO",MB_OK|MB_ICONEXCLAMATION);
		}
	
	
		ShowWindow(main,SW_SHOW);						// Show The Window

		
    
	MSG msg;

    while(TRUE)
    {
        while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }

        if(msg.message == WM_QUIT)
            break;
			


		DrawGLScene();

    }

	
    return msg.wParam;
}


LRESULT CALLBACK WindowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	

    switch(message)
    {
		case WM_DESTROY:
            {
                PostQuitMessage(0);
                return 0;
            } break;
		case WM_SIZING:
			{
                                //if to comment this out-error is gone!
				DrawGLScene();
			} break;
		case WM_SIZE:
			{
                                //if to comment this out-error is gone!
				DrawGLScene();
			} break;
    }

    return DefWindowProc (hWnd, message, wParam, lParam);
}

void DrawGLScene(GLvoid)                             // Here's Where We Do All The Drawing
{
	
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);         
	glLoadIdentity();                           
	

	int c=newtest.take();
	
	if (c==10)
	{
		MessageBox(NULL,L"take from gl working!",L"INFO",MB_OK|MB_ICONEXCLAMATION);
	}
	
	glFlush();
}




... so as far as i understand the function call to newtest.take() was done before it was initialized so it generated error. TYPE type[0].x was = NULL... Correct me if i'm wrong.
Last edited on
Topic archived. No new replies allowed.