declaring TBitmap globally












0














I would like to declare a TBitmap Globally.



I tried as follows:



Locally within a method, this works fine



std::auto_ptr<Graphics::TBitmap> RenderGraphic(new Graphics::TBitmap());


OR



Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;


So to declare it globally I tried this in the header file



Graphics::TBitmap *RenderGraphic;


And this in the constructor



__fastcall TShipGraphic::TShipGraphic(TComponent* Owner)
: TForm(Owner)

{

Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;

}


Which compiles fine but when running, throws an access violation exception at the first occurrence of



      RenderGraphic->Canvas->Pen->Color = clBlack;


Please advise, tks in advance.



The reference source I was using is C++ Builder Graphics Introduction



which suggested the declaration in the constructor










share|improve this question


















  • 1




    In the constructor you are declaring and initializing a local variable and not the global variable RenderGraphic
    – Kerem D
    Nov 12 '18 at 12:39












  • Global to what? You got more than one Forms? using Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; in main form cpp somwhere at the top usually works but ... if you are accessing it from different forms it might get duplicated unless you use static. Much safer is to have it as a member and share its pointer when ever you need to. The access violation is usually at the start when you are accessing things not yet initialized. We need to know more about architecture of your app ... Hope you are not using threads ...
    – Spektre
    Nov 13 '18 at 4:38












  • @Spektra In this instance global across all methods within ONE form.
    – BarryK
    Nov 13 '18 at 6:42










  • I placed Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; near the top of the .cpp, outside of the Constructor, or any other method, and now don't get Access Violation errors, however all my RenderGraphic->Canvas-> statements, like MoveTo, LineTo, Polygon appear to do nothing as no bitmap is drawn to the canvas, when using Canvas->Draw(10, 10, RenderGraphic); and when saving to a .bmp file, the file size is zero. Perhaps this is a different question.
    – BarryK
    Nov 13 '18 at 7:06












  • Ok I have worked the bitmap size was probably zero hence nothing appeared. I established this being to draw over an existing bitmap loaded from file. Rendergraphic->SetSize sorts out this problem. But in order to draw transparently over what is already on the form canvas, I still need to first load a bitmap, albeit a plain white one. RenderGraphic->Transparent = true, has no effect unless a bitmap has been loaded from file. Any advise here?
    – BarryK
    Nov 13 '18 at 12:50
















0














I would like to declare a TBitmap Globally.



I tried as follows:



Locally within a method, this works fine



std::auto_ptr<Graphics::TBitmap> RenderGraphic(new Graphics::TBitmap());


OR



Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;


So to declare it globally I tried this in the header file



Graphics::TBitmap *RenderGraphic;


And this in the constructor



__fastcall TShipGraphic::TShipGraphic(TComponent* Owner)
: TForm(Owner)

{

Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;

}


Which compiles fine but when running, throws an access violation exception at the first occurrence of



      RenderGraphic->Canvas->Pen->Color = clBlack;


Please advise, tks in advance.



The reference source I was using is C++ Builder Graphics Introduction



which suggested the declaration in the constructor










share|improve this question


















  • 1




    In the constructor you are declaring and initializing a local variable and not the global variable RenderGraphic
    – Kerem D
    Nov 12 '18 at 12:39












  • Global to what? You got more than one Forms? using Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; in main form cpp somwhere at the top usually works but ... if you are accessing it from different forms it might get duplicated unless you use static. Much safer is to have it as a member and share its pointer when ever you need to. The access violation is usually at the start when you are accessing things not yet initialized. We need to know more about architecture of your app ... Hope you are not using threads ...
    – Spektre
    Nov 13 '18 at 4:38












  • @Spektra In this instance global across all methods within ONE form.
    – BarryK
    Nov 13 '18 at 6:42










  • I placed Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; near the top of the .cpp, outside of the Constructor, or any other method, and now don't get Access Violation errors, however all my RenderGraphic->Canvas-> statements, like MoveTo, LineTo, Polygon appear to do nothing as no bitmap is drawn to the canvas, when using Canvas->Draw(10, 10, RenderGraphic); and when saving to a .bmp file, the file size is zero. Perhaps this is a different question.
    – BarryK
    Nov 13 '18 at 7:06












  • Ok I have worked the bitmap size was probably zero hence nothing appeared. I established this being to draw over an existing bitmap loaded from file. Rendergraphic->SetSize sorts out this problem. But in order to draw transparently over what is already on the form canvas, I still need to first load a bitmap, albeit a plain white one. RenderGraphic->Transparent = true, has no effect unless a bitmap has been loaded from file. Any advise here?
    – BarryK
    Nov 13 '18 at 12:50














0












0








0







I would like to declare a TBitmap Globally.



I tried as follows:



Locally within a method, this works fine



std::auto_ptr<Graphics::TBitmap> RenderGraphic(new Graphics::TBitmap());


OR



Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;


So to declare it globally I tried this in the header file



Graphics::TBitmap *RenderGraphic;


And this in the constructor



__fastcall TShipGraphic::TShipGraphic(TComponent* Owner)
: TForm(Owner)

{

Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;

}


Which compiles fine but when running, throws an access violation exception at the first occurrence of



      RenderGraphic->Canvas->Pen->Color = clBlack;


Please advise, tks in advance.



The reference source I was using is C++ Builder Graphics Introduction



which suggested the declaration in the constructor










share|improve this question













I would like to declare a TBitmap Globally.



I tried as follows:



Locally within a method, this works fine



std::auto_ptr<Graphics::TBitmap> RenderGraphic(new Graphics::TBitmap());


OR



Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;


So to declare it globally I tried this in the header file



Graphics::TBitmap *RenderGraphic;


And this in the constructor



__fastcall TShipGraphic::TShipGraphic(TComponent* Owner)
: TForm(Owner)

{

Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap;

}


Which compiles fine but when running, throws an access violation exception at the first occurrence of



      RenderGraphic->Canvas->Pen->Color = clBlack;


Please advise, tks in advance.



The reference source I was using is C++ Builder Graphics Introduction



which suggested the declaration in the constructor







c++builder rad-studio






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 12 '18 at 9:44









BarryKBarryK

94




94








  • 1




    In the constructor you are declaring and initializing a local variable and not the global variable RenderGraphic
    – Kerem D
    Nov 12 '18 at 12:39












  • Global to what? You got more than one Forms? using Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; in main form cpp somwhere at the top usually works but ... if you are accessing it from different forms it might get duplicated unless you use static. Much safer is to have it as a member and share its pointer when ever you need to. The access violation is usually at the start when you are accessing things not yet initialized. We need to know more about architecture of your app ... Hope you are not using threads ...
    – Spektre
    Nov 13 '18 at 4:38












  • @Spektra In this instance global across all methods within ONE form.
    – BarryK
    Nov 13 '18 at 6:42










  • I placed Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; near the top of the .cpp, outside of the Constructor, or any other method, and now don't get Access Violation errors, however all my RenderGraphic->Canvas-> statements, like MoveTo, LineTo, Polygon appear to do nothing as no bitmap is drawn to the canvas, when using Canvas->Draw(10, 10, RenderGraphic); and when saving to a .bmp file, the file size is zero. Perhaps this is a different question.
    – BarryK
    Nov 13 '18 at 7:06












  • Ok I have worked the bitmap size was probably zero hence nothing appeared. I established this being to draw over an existing bitmap loaded from file. Rendergraphic->SetSize sorts out this problem. But in order to draw transparently over what is already on the form canvas, I still need to first load a bitmap, albeit a plain white one. RenderGraphic->Transparent = true, has no effect unless a bitmap has been loaded from file. Any advise here?
    – BarryK
    Nov 13 '18 at 12:50














  • 1




    In the constructor you are declaring and initializing a local variable and not the global variable RenderGraphic
    – Kerem D
    Nov 12 '18 at 12:39












  • Global to what? You got more than one Forms? using Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; in main form cpp somwhere at the top usually works but ... if you are accessing it from different forms it might get duplicated unless you use static. Much safer is to have it as a member and share its pointer when ever you need to. The access violation is usually at the start when you are accessing things not yet initialized. We need to know more about architecture of your app ... Hope you are not using threads ...
    – Spektre
    Nov 13 '18 at 4:38












  • @Spektra In this instance global across all methods within ONE form.
    – BarryK
    Nov 13 '18 at 6:42










  • I placed Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; near the top of the .cpp, outside of the Constructor, or any other method, and now don't get Access Violation errors, however all my RenderGraphic->Canvas-> statements, like MoveTo, LineTo, Polygon appear to do nothing as no bitmap is drawn to the canvas, when using Canvas->Draw(10, 10, RenderGraphic); and when saving to a .bmp file, the file size is zero. Perhaps this is a different question.
    – BarryK
    Nov 13 '18 at 7:06












  • Ok I have worked the bitmap size was probably zero hence nothing appeared. I established this being to draw over an existing bitmap loaded from file. Rendergraphic->SetSize sorts out this problem. But in order to draw transparently over what is already on the form canvas, I still need to first load a bitmap, albeit a plain white one. RenderGraphic->Transparent = true, has no effect unless a bitmap has been loaded from file. Any advise here?
    – BarryK
    Nov 13 '18 at 12:50








1




1




In the constructor you are declaring and initializing a local variable and not the global variable RenderGraphic
– Kerem D
Nov 12 '18 at 12:39






In the constructor you are declaring and initializing a local variable and not the global variable RenderGraphic
– Kerem D
Nov 12 '18 at 12:39














Global to what? You got more than one Forms? using Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; in main form cpp somwhere at the top usually works but ... if you are accessing it from different forms it might get duplicated unless you use static. Much safer is to have it as a member and share its pointer when ever you need to. The access violation is usually at the start when you are accessing things not yet initialized. We need to know more about architecture of your app ... Hope you are not using threads ...
– Spektre
Nov 13 '18 at 4:38






Global to what? You got more than one Forms? using Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; in main form cpp somwhere at the top usually works but ... if you are accessing it from different forms it might get duplicated unless you use static. Much safer is to have it as a member and share its pointer when ever you need to. The access violation is usually at the start when you are accessing things not yet initialized. We need to know more about architecture of your app ... Hope you are not using threads ...
– Spektre
Nov 13 '18 at 4:38














@Spektra In this instance global across all methods within ONE form.
– BarryK
Nov 13 '18 at 6:42




@Spektra In this instance global across all methods within ONE form.
– BarryK
Nov 13 '18 at 6:42












I placed Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; near the top of the .cpp, outside of the Constructor, or any other method, and now don't get Access Violation errors, however all my RenderGraphic->Canvas-> statements, like MoveTo, LineTo, Polygon appear to do nothing as no bitmap is drawn to the canvas, when using Canvas->Draw(10, 10, RenderGraphic); and when saving to a .bmp file, the file size is zero. Perhaps this is a different question.
– BarryK
Nov 13 '18 at 7:06






I placed Graphics::TBitmap * RenderGraphic = new Graphics::TBitmap; near the top of the .cpp, outside of the Constructor, or any other method, and now don't get Access Violation errors, however all my RenderGraphic->Canvas-> statements, like MoveTo, LineTo, Polygon appear to do nothing as no bitmap is drawn to the canvas, when using Canvas->Draw(10, 10, RenderGraphic); and when saving to a .bmp file, the file size is zero. Perhaps this is a different question.
– BarryK
Nov 13 '18 at 7:06














Ok I have worked the bitmap size was probably zero hence nothing appeared. I established this being to draw over an existing bitmap loaded from file. Rendergraphic->SetSize sorts out this problem. But in order to draw transparently over what is already on the form canvas, I still need to first load a bitmap, albeit a plain white one. RenderGraphic->Transparent = true, has no effect unless a bitmap has been loaded from file. Any advise here?
– BarryK
Nov 13 '18 at 12:50




Ok I have worked the bitmap size was probably zero hence nothing appeared. I established this being to draw over an existing bitmap loaded from file. Rendergraphic->SetSize sorts out this problem. But in order to draw transparently over what is already on the form canvas, I still need to first load a bitmap, albeit a plain white one. RenderGraphic->Transparent = true, has no effect unless a bitmap has been loaded from file. Any advise here?
– BarryK
Nov 13 '18 at 12:50












1 Answer
1






active

oldest

votes


















0














You need to implement a singleton. Consider read-only case (bitmap is created once, no setter function).



In MyGraphics.h define accessor function



#include <vcl.h>
TBitmap* GetRenderGraphic();


Implementation in MyGraphics.cpp



static std::unique_ptr<TBitmap> renderGraphic(new TBitmap());

TBitmap* GetRenderGraphic()
{
return renderGraphic.get();
}


Use it (required including of MyGraphics.h)



GetRenderGraphic()->Canvas->Pen->Color = clBlack;





share|improve this answer





















  • Many Thanks @serge . I did as suggested, however I get a linker error [ilink32 Error] Error: Unresolved external 'TShipGraphic::GetRenderGraphic()' referenced from E:!STAB PROGRAMBARRACUDA 2018E - SG CODEBARRACUDA 2018E2WIN32DEBUGOSSSHIPGRAPHIC.OBJ [ilink32 Error] Error: Unable to perform link
    – BarryK
    Nov 12 '18 at 13:43












  • TShipGraphic::GetRenderGraphic() seems to be not implemented or the implementation is not included in the project. It should be marked as static also as you use class method to access static member.
    – serge
    Nov 12 '18 at 14:15













Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259456%2fdeclaring-tbitmap-globally%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You need to implement a singleton. Consider read-only case (bitmap is created once, no setter function).



In MyGraphics.h define accessor function



#include <vcl.h>
TBitmap* GetRenderGraphic();


Implementation in MyGraphics.cpp



static std::unique_ptr<TBitmap> renderGraphic(new TBitmap());

TBitmap* GetRenderGraphic()
{
return renderGraphic.get();
}


Use it (required including of MyGraphics.h)



GetRenderGraphic()->Canvas->Pen->Color = clBlack;





share|improve this answer





















  • Many Thanks @serge . I did as suggested, however I get a linker error [ilink32 Error] Error: Unresolved external 'TShipGraphic::GetRenderGraphic()' referenced from E:!STAB PROGRAMBARRACUDA 2018E - SG CODEBARRACUDA 2018E2WIN32DEBUGOSSSHIPGRAPHIC.OBJ [ilink32 Error] Error: Unable to perform link
    – BarryK
    Nov 12 '18 at 13:43












  • TShipGraphic::GetRenderGraphic() seems to be not implemented or the implementation is not included in the project. It should be marked as static also as you use class method to access static member.
    – serge
    Nov 12 '18 at 14:15


















0














You need to implement a singleton. Consider read-only case (bitmap is created once, no setter function).



In MyGraphics.h define accessor function



#include <vcl.h>
TBitmap* GetRenderGraphic();


Implementation in MyGraphics.cpp



static std::unique_ptr<TBitmap> renderGraphic(new TBitmap());

TBitmap* GetRenderGraphic()
{
return renderGraphic.get();
}


Use it (required including of MyGraphics.h)



GetRenderGraphic()->Canvas->Pen->Color = clBlack;





share|improve this answer





















  • Many Thanks @serge . I did as suggested, however I get a linker error [ilink32 Error] Error: Unresolved external 'TShipGraphic::GetRenderGraphic()' referenced from E:!STAB PROGRAMBARRACUDA 2018E - SG CODEBARRACUDA 2018E2WIN32DEBUGOSSSHIPGRAPHIC.OBJ [ilink32 Error] Error: Unable to perform link
    – BarryK
    Nov 12 '18 at 13:43












  • TShipGraphic::GetRenderGraphic() seems to be not implemented or the implementation is not included in the project. It should be marked as static also as you use class method to access static member.
    – serge
    Nov 12 '18 at 14:15
















0












0








0






You need to implement a singleton. Consider read-only case (bitmap is created once, no setter function).



In MyGraphics.h define accessor function



#include <vcl.h>
TBitmap* GetRenderGraphic();


Implementation in MyGraphics.cpp



static std::unique_ptr<TBitmap> renderGraphic(new TBitmap());

TBitmap* GetRenderGraphic()
{
return renderGraphic.get();
}


Use it (required including of MyGraphics.h)



GetRenderGraphic()->Canvas->Pen->Color = clBlack;





share|improve this answer












You need to implement a singleton. Consider read-only case (bitmap is created once, no setter function).



In MyGraphics.h define accessor function



#include <vcl.h>
TBitmap* GetRenderGraphic();


Implementation in MyGraphics.cpp



static std::unique_ptr<TBitmap> renderGraphic(new TBitmap());

TBitmap* GetRenderGraphic()
{
return renderGraphic.get();
}


Use it (required including of MyGraphics.h)



GetRenderGraphic()->Canvas->Pen->Color = clBlack;






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 12 '18 at 13:09









sergeserge

59537




59537












  • Many Thanks @serge . I did as suggested, however I get a linker error [ilink32 Error] Error: Unresolved external 'TShipGraphic::GetRenderGraphic()' referenced from E:!STAB PROGRAMBARRACUDA 2018E - SG CODEBARRACUDA 2018E2WIN32DEBUGOSSSHIPGRAPHIC.OBJ [ilink32 Error] Error: Unable to perform link
    – BarryK
    Nov 12 '18 at 13:43












  • TShipGraphic::GetRenderGraphic() seems to be not implemented or the implementation is not included in the project. It should be marked as static also as you use class method to access static member.
    – serge
    Nov 12 '18 at 14:15




















  • Many Thanks @serge . I did as suggested, however I get a linker error [ilink32 Error] Error: Unresolved external 'TShipGraphic::GetRenderGraphic()' referenced from E:!STAB PROGRAMBARRACUDA 2018E - SG CODEBARRACUDA 2018E2WIN32DEBUGOSSSHIPGRAPHIC.OBJ [ilink32 Error] Error: Unable to perform link
    – BarryK
    Nov 12 '18 at 13:43












  • TShipGraphic::GetRenderGraphic() seems to be not implemented or the implementation is not included in the project. It should be marked as static also as you use class method to access static member.
    – serge
    Nov 12 '18 at 14:15


















Many Thanks @serge . I did as suggested, however I get a linker error [ilink32 Error] Error: Unresolved external 'TShipGraphic::GetRenderGraphic()' referenced from E:!STAB PROGRAMBARRACUDA 2018E - SG CODEBARRACUDA 2018E2WIN32DEBUGOSSSHIPGRAPHIC.OBJ [ilink32 Error] Error: Unable to perform link
– BarryK
Nov 12 '18 at 13:43






Many Thanks @serge . I did as suggested, however I get a linker error [ilink32 Error] Error: Unresolved external 'TShipGraphic::GetRenderGraphic()' referenced from E:!STAB PROGRAMBARRACUDA 2018E - SG CODEBARRACUDA 2018E2WIN32DEBUGOSSSHIPGRAPHIC.OBJ [ilink32 Error] Error: Unable to perform link
– BarryK
Nov 12 '18 at 13:43














TShipGraphic::GetRenderGraphic() seems to be not implemented or the implementation is not included in the project. It should be marked as static also as you use class method to access static member.
– serge
Nov 12 '18 at 14:15






TShipGraphic::GetRenderGraphic() seems to be not implemented or the implementation is not included in the project. It should be marked as static also as you use class method to access static member.
– serge
Nov 12 '18 at 14:15




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53259456%2fdeclaring-tbitmap-globally%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Full-time equivalent

さくらももこ

13 indicted, 8 arrested in Calif. drug cartel investigation